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/questions/21990659/replace-string-pattern-advanced.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
Last part of the trilogy "Lord of the Code", as a sequel to https://www.experts-exchange.com/questions/21990659/replace-string-pattern-advanced.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
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!
I cannot test it tonight, probably tomorrow or Monday (now working on my social life *grin*)
I'll be back!
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
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
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++).'">'.$res ult[1][$i] .'</a>';
$texts.='<a name="p'.($a).'">'.$result [2][$i].'< /a>';
}
$output=$links.'<br><br>'. $texts;
echo htmlentities($output);
?>
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\\](
$a=0;
echo "<pre>";
print_r($result);
echo "</pre>";
for ($i = 0; $i < count($result[1]); $i++) {
$links.= '<a href="#p'.($a++).'">'.$res
$texts.='<a name="p'.($a).'">'.$result
}
$output=$links.'<br><br>'.
echo htmlentities($output);
?>
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
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
$html='[ch]First paragraph[/ch]
Some text
[ch]Second paragraph[/ch]
Another line';
can you send me your code please? we miss something...
Some text
[ch]Second paragraph[/ch]
Another line';
can you send me your code please? we miss something...
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++).'">'.$res ult[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....
<?php
$html='[ch]First paragraph[/ch]
Some text
[ch]Second paragraph[/ch]
Another line';
$links='';
$texts='';
preg_match_all('/\\[ch\\](
$a=0;
echo "<pre>";
print_r($result);
echo "</pre>";
for ($i = 0; $i < count($result[1]); $i++) {
$links.= '<a href="#p'.($a++).'">'.$res
$texts.='<a name="p'.($a).'">'.$result
}
$output=$links.'<br><br>'.
echo htmlentities($output);
?>
I don't see where it goes wrong....
I changed PREG_PATTERN_ORDER=>PREG_S ET_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++).'">'.$res ult[$i][1] .'</a>';
$texts.='<a name="p'.($a).'">'.$result [$i][2].'< /a>';
}
$output=$links.'<br><br>'. $texts;
echo htmlentities($output);
?>
<?php
$html='[ch]First paragraph[/ch]
Some text
[ch]Second paragraph[/ch]
Another line';
$links='';
$texts='';
preg_match_all('/\\[ch\\](
for ($i = 0; $i < count($result); $i++) {
$links.= '<a href="#p'.($a++).'">'.$res
$texts.='<a name="p'.($a).'">'.$result
}
$output=$links.'<br><br>'.
echo htmlentities($output);
?>
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).'">'.$res ult[$i][1] .'</a>';
$texts.='<a name="p'.($i+1).'">'.$resu lt[$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?
<?php
$html='[ch]First paragraph[/ch]
Some text
[ch]Second paragraph[/ch]
Another line';
$links='';
$texts='';
preg_match_all('/\\[ch]([^
for ($i=0; $i < count($result); $i++) {
$links.= '<a href="#p'.($i+1).'">'.$res
$texts.='<a name="p'.($i+1).'">'.$resu
}
$output=$links.'<br><br>'.
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?
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++).'">'.$res ult[$i][1] .'</a>';
$texts.='<a name="p'.($a).'">'.$result [$i][2].'< /a>';
}
$output=$links.'<br><br>'. $texts;
echo htmlentities($output);
?>
<?php
$html='[ch]First paragraph[/ch]
Some text
[ch]Second paragraph[/ch]
Another line';
$links='';
$texts='';
$a=0;
preg_match_all('/\\[ch\\](
for ($i = 0; $i < count($result); $i++) {
$links.= '<a href="#p'.($a++).'">'.$res
$texts.='<a name="p'.($a).'">'.$result
}
$output=$links.'<br><br>'.
echo htmlentities($output);
?>
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.
??
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.
??
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...
WORKING CODE
'/\\[ch\\]([^\\[]*)\\[\/ch
MY FIRST CODE is false
'/\\[ch]([^\\[]*)\\[\/ch\\
^
|
i forget \\ before if you use the regexp, it wont work...
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++).'">'.$res ult[$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!)
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\\](
for ($i = 0; $i < count($result); $i++) {
$links.= '<a href="#p'.($a++).'">'.$res
$texts.='<a name="p'.($a).'">'.$result
}
$output=$links.'<br><br>'.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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).'">'.$resul t[$i][1].' </a><br />';
$texts.='<a name="p'.($a).'"><b>'.$res ult[$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!
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\\](
for ($i = 0; $i < count($result); $i++) {
$links.= '<a href="#p'.($a).'">'.$resul
$texts.='<a name="p'.($a).'"><b>'.$res
$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!
\\[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);
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\\](
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)?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Tolga, could you please send your mail address to pieter[at]kubben[dot]nl...
Thanks for all your help so far!
Pieter
<?php
$html='[ch]First paragraph[/ch]
Some text
[ch]Second paragraph[/ch]
Another line';
$links='';
$texts='';
preg_match_all('/\\[ch]([^
for ($i=0; $i < count($result); $i++) {
$links.= '<a href="#p'.($i+1).'">'.$res
$texts.='<a name="p'.($i+1).'">'.$resu
}
$output=$links.'<br><br>'.
echo htmlentities($output);
?>