Avatar of pieter78
pieter78
 asked on

string thing - the trilogy completed

Hi,

Last part of the trilogy "Lord of the Code", as a sequel to https://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21990659.html#17530491

Everything works so far, so the things I wanted to do are done. However, I got one idea later on, perhaps it is possible as well... The commands [b], [i], [link] and [img] are working (Tolga, thanks). The last idea is this:

USER INPUT
----------------------------
[ch]First paragraph[/ch]
Some text

[ch]Second paragraph[/ch]
Another line
----------------------------

should be converted to

----------------------------
<a href="#p1">First paragraph</a>
<a href="#p2">Second paragraph</a>
<br /><br />
<a name="p1">Some text

<a name="p2">Another line
----------------------------

If possible, I'd be glad to know!

Pieter
PHP

Avatar of undefined
Last Comment
pieter78

8/22/2022 - Mon
tolgaong

Thanks Pieter. ;)

<?php
$html='[ch]First paragraph[/ch]
Some text

[ch]Second paragraph[/ch]
Another line';
$links='';
$texts='';
preg_match_all('/\\[ch]([^\\[]*)\\[\/ch\\]\\r\\n([^\\r\\n]*)/', $html, $result, PREG_SET_ORDER);
for ($i=0; $i < count($result); $i++) {
            $links.= '<a href="#p'.($i+1).'">'.$result[$i][1].'</a>';
            $texts.='<a name="p'.($i+1).'">'.$result[$i][2].'</a>';
      }
$output=$links.'<br><br>'.$texts;
echo htmlentities($output);
?>
pieter78

ASKER
Thanks, this looks really promising... You really surprise me every time again by how simple your code looks compared to mine! :-)

I cannot test it tonight, probably tomorrow or Monday (now working on my social life *grin*)

I'll be back!
pieter78

ASKER
Hi Tolga,

I tried tonight, but it does not seem to work. All it displays, is "<br><br>" but nothing of the content.

Could I do something wrong, or might there be something with the script?

Kind regards,

Pieter
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
tolgaong

Sorry Pieter i forgot a \\ before ] it worked at my comp.
I hope you will use it successully. And i add another feature it is incasesensitive so you can add [Ch] [CH] [cH] [ch]

<?php
$html='[ch]First paragraph[/ch]
Some text

[ch]Second paragraph[/ch]
Another line';
$links='';
$texts='';
preg_match_all('/\\[ch\\]([^\\[]*)\\[\/ch\\]\\r\\n([^\\r\\n]*)/i', $html, $result, PREG_PATTERN_ORDER);
$a=0;
echo "<pre>";
print_r($result);
echo "</pre>";
for ($i = 0; $i < count($result[1]); $i++) {
            $links.= '<a href="#p'.($a++).'">'.$result[1][$i].'</a>';
            $texts.='<a name="p'.($a).'">'.$result[2][$i].'</a>';
      }
$output=$links.'<br><br>'.$texts;
echo htmlentities($output);
?>
pieter78

ASKER
Hi,

Thanks... but now my browser displays this:

-------------
Array
(
    [0] => Array
        (
        )

    [1] => Array
        (
        )

    [2] => Array
        (
        )

)

<br><br>
--------------

In some way, it looks like the array content still does not be what it should be.

Any inspiration left?

Thx! Pieter
tolgaong

$html='[ch]First paragraph[/ch]
Some text

[ch]Second paragraph[/ch]
Another line';

can you send me your code please? we miss something...
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
pieter78

ASKER
This is the code as I pasted it in the script:

<?php
$html='[ch]First paragraph[/ch]
Some text

[ch]Second paragraph[/ch]
Another line';
$links='';
$texts='';
preg_match_all('/\\[ch\\]([^\\[]*)\\[\/ch\\]\\r\\n([^\\r\\n]*)/i', $html, $result, PREG_PATTERN_ORDER);
$a=0;
echo "<pre>";
print_r($result);
echo "</pre>";
for ($i = 0; $i < count($result[1]); $i++) {
          $links.= '<a href="#p'.($a++).'">'.$result[1][$i].'</a>';
          $texts.='<a name="p'.($a).'">'.$result[2][$i].'</a>';
     }
$output=$links.'<br><br>'.$texts;
echo htmlentities($output);
?>

I don't see where it goes wrong....
tolgaong

I changed PREG_PATTERN_ORDER=>PREG_SET_ORDER

<?php
$html='[ch]First paragraph[/ch]
Some text

[ch]Second paragraph[/ch]
Another line';
$links='';
$texts='';
preg_match_all('/\\[ch\\]([^\\[]*)\\[\/ch\\]\\r\\n([^\\r\\n]*)/', $html, $result, PREG_SET_ORDER);
for ($i = 0; $i < count($result); $i++) {
    $links.= '<a href="#p'.($a++).'">'.$result[$i][1].'</a>';
    $texts.='<a name="p'.($a).'">'.$result[$i][2].'</a>';      
      }
$output=$links.'<br><br>'.$texts;
echo htmlentities($output);
?>
pieter78

ASKER
We are getting closer... the last script you provided still did not work and only showed <br><br> as output... However, I tried the opposite change in the first script, which now looks like this:

<?php
$html='[ch]First paragraph[/ch]
Some text

[ch]Second paragraph[/ch]
Another line';
$links='';
$texts='';
preg_match_all('/\\[ch]([^\\[]*)\\[\/ch\\]\\r\\n([^\\r\\n]*)/', $html, $result, PREG_PATTERN_ORDER);
for ($i=0; $i < count($result); $i++) {
          $links.= '<a href="#p'.($i+1).'">'.$result[$i][1].'</a>';
          $texts.='<a name="p'.($i+1).'">'.$result[$i][2].'</a>';
     }
$output=$links.'<br><br>'.$texts;
echo htmlentities($output);
?>

This now gives as outcome:

<a href="#p1"></a><a href="#p2"></a><a href="#p3"></a><br><br><a name="p1"></a><a name="p2"></a><a name="p3"></a>

So it recognizes $links and $texts, but (1) does count one item too much, and (2) most important: it does not fill these variables using the $result array. I guess there is something wrong with $result, but I could not get this to work...

Does this help?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
tolgaong

My last code has some changes on it. It's different from your last comment. If you paste my new code below... You will succeded ;)

<?php
$html='[ch]First paragraph[/ch]
Some text

[ch]Second paragraph[/ch]
Another line';
$links='';
$texts='';
$a=0;
preg_match_all('/\\[ch\\]([^\\[]*)\\[\/ch\\]\\r\\n([^\\r\\n]*)/', $html, $result, PREG_SET_ORDER);
for ($i = 0; $i < count($result); $i++) {
    $links.= '<a href="#p'.($a++).'">'.$result[$i][1].'</a>';
    $texts.='<a name="p'.($a).'">'.$result[$i][2].'</a>';    
     }
$output=$links.'<br><br>'.$texts;
echo htmlentities($output);
?>

pieter78

ASKER
I guess I am getting crazy... ;-)

If I use your code exactly as displayed, I get again <br><br> as outcome. If I change PREG_SET_ORDER in PREG_PATTERN_ORDER then it displays <a href="#p0"></a><a href="#p1"></a><a href="#p2"></a><br><br><a name="p1"></a><a name="p2"></a><a name="p3"></a>

So still, $result is not really used. I tested running a localhost on Mac OSX, and my online Linux host.

??
tolgaong

this code  has to work
WORKING CODE
'/\\[ch\\]([^\\[]*)\\[\/ch\\]\\r\\n([^\\r\\n]*)/'

MY FIRST CODE is false
'/\\[ch]([^\\[]*)\\[\/ch\\]\\r\\n([^\\r\\n]*)/'
        ^
         |
i forget \\ before if you use the regexp, it wont work...
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
pieter78

ASKER
As I said, I guess I am getting crazy....

Now I have this code, which should work:
------------------------------------------------------------------------
<?php
$html='[ch]First paragraph[/ch]
Some text

[ch]Second paragraph[/ch]
Another line';
$links='';
$texts='';
$a=0;
preg_match_all('/\\[ch\\]([^\\[]*)\\[\/ch\\]\\r\\n([^\\r\\n]*)/', $html, $result, PREG_SET_ORDER);
for ($i = 0; $i < count($result); $i++) {
    $links.= '<a href="#p'.($a++).'">'.$result[$i][1].'</a>';
    $texts.='<a name="p'.($a).'">'.$result[$i][2].'</a>';    
     }
$output=$links.'<br><br>'.$texts;
echo htmlentities($output);
?>
------------------------------------------------------------------------

However, I just get <br><br> and changing SET to PATTERN changes it to

<a href="#p0"></a><a href="#p1"></a><a href="#p2"></a><br><br><a name="p1"></a><a name="p2"></a><a name="p3"></a>

but still the text is not displayed.

I used the code exactly as displayed about... (without the ---------- of course)

What could be going wrong?

(tested it on two servers, one Mac and one Linux - all the other RegExp work!)
ASKER CERTIFIED SOLUTION
tolgaong

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
pieter78

ASKER
Great! This one is doing what is was supposed to do, just one little thing:

The previous code (from the previous two topics) had a very nice feature, which is that if the user makes a mistake (e.g. forgets a letter, forgets the closing slash) the code is just displayed, but not "executed".

If I do this here, the whole paragraph disappears.....

CODE:

<?php
$html="[ch]First paragraph[/ch]
Some text

[ch]Second paragraph[/ch]
Another line

[ch]Third paragraph[/ch]
Added for testing
";

$links='';
$texts='';
$a=1;
preg_match_all('/\\[ch\\]([^\\[]*)\\[\/ch\\]\\n([^\\n]*)/', $html, $result, PREG_SET_ORDER);
for ($i = 0; $i < count($result); $i++) {
    $links.= '<a href="#p'.($a).'">'.$result[$i][1].'</a><br />';
    $texts.='<a name="p'.($a).'"><b>'.$result[$i][1].'</b></a><br />'.$result[$i][2].'<br /><br />';
    $a++;
     }
$output=$links.'<br /><br />'.$texts;
// echo htmlentities($output);
echo $output;
?>

Is it also possible to display incorrect code just as the text that was typed?

Like: [ch]Some text[ch] or [ch]Another text[/c]??

That should finish this question, I guess... Thanks!
tolgaong

\\[ch\\]([^\\[]*)\\[\/ch\\]\\n([^\\n]*) to      \\[ch\\]([^\\[]*)\\[[\/a-z]*\\]\\n([^\\n]*)


it will works even
[ch]some text[] or [ch]sometext[ch] or [ch]sometext[/ch] or whatelse [(a-z and /) OR NO CHAR]

preg_match_all('/\\[ch\\]([^\\[]*)\\[[\/a-z]*\\]\\n([^\\n]*)/', $html, $result, PREG_SET_ORDER);
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
pieter78

ASKER
I tried it, and you are right... but there is one thing:

if somebody enters [ch]Title and forgets the [] at all, then the paragraph still disappears.

If possible, I'd prefer to have incomplete code displayed as literal text (so in this case just [ch]Title)... ) instead of being executed.

Is there a way to check if the closing tag is present (this worked in the previous two topics, including the [link] tag)?
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
pieter78

ASKER
Well, the question I placed has been answered, so I guess it is fair to close this question.

Tolga, could you please send your mail address to pieter[at]kubben[dot]nl... at the moment I am rather busy with some other things, but I guess I will come back onto this topic for several details (like the closing tags or case insensitivity), then I can point you towards the new question.

Thanks for all your help so far!

Pieter