Page 1 of 2
[SOLVED]highlight_string, but not all?
Posted: Sat Jun 05, 2004 9:45 pm
by Steveo31
Heyo.
In my script, I have a DB entry that contains htmlspecialchar()d text, both HTML and PHP. I finally got it to be htmlspecialchar so that form elements and whatnot are not parsed AND have a code block like the forums here so that desired code is shown correctly. Problem is, I would like to highlight the area that is inside the [div] tag I have made (hence, showing the code).
If I hightlight_string the entire entry, duh it'll come up wrong. I've been pondering this for a bit and figured someone here can help. Ya'll always do

Posted: Sat Jun 05, 2004 11:19 pm
by feyd
I've done this before (untested code):
Code: Select all
preg_replace('|\[div](.*?)\[/div]|ie','highlight_string("\\1",true)',$allLines);
Posted: Sun Jun 06, 2004 12:26 am
by Steveo31
AAAAH!!! Man, that preg_replace code is killin me!!
I'll try that out when my blood pressure gets below 200

Posted: Tue Jun 08, 2004 2:59 pm
by Steveo31
Ok, I tried it and some variations of it now that I understand (sorta) the preg_replace code. Now, on phpfreaks.com, there is a tut that google came back with. It said that the subsections matched in the first part of the code are refered to in the second parameter of the function with an escaped $, then the number slot. So
Code: Select all
preg_replace("|(.+)\[div\](.+)\[/div\](.+)|s", "highlight_string('\$1', true)", $string);
Would make the first parenthesized subsection (.+) be highlighted. Correct?
Anyway, it didn't work the way I had hoped, so could someone clear this \\1 thing up? Thanks guys

Posted: Tue Jun 08, 2004 3:05 pm
by feyd
\\1 is the same as \$1 last I checked.
Posted: Tue Jun 08, 2004 6:36 pm
by Steveo31
Hm, ok. Thanks feyd
Well, I gave it a try and here's the code that works:
Code: Select all
echo preg_replace("|^(.*)\[div\]?(.*)\[/div\]?(.*)|s", "<div class='innercode'>".highlight_string("\$2", true)."</div>", $row['body']);
This makes the div class there, in my case a background color, etc., but the code in the database won't show. It gets parsed. It's stored in a TEXT database, not htmlspecialchar()d.
Posted: Tue Jun 08, 2004 7:07 pm
by feyd
the way that codes set it'll replace everything in the body with the div if [div and [/div exist.
Posted: Tue Jun 08, 2004 7:22 pm
by Steveo31
Really? Weird... I thought you had to escape [ and ] cause they are "metacharacters" or whatnot... I'll take out the \] and see how it goes.
Thanks for comin back feyd.
Posted: Tue Jun 08, 2004 8:15 pm
by feyd
it's the ? after each \] that'll screw with it.. for that bit.. and the replace all will happen because of the first (.*) and the last (.*)
additionally.. if you have multiple [div] bits.. it may replace entire sections..
the following:
Code: Select all
<?php
$allLines = 'this is a test of the div tagging... [div]<?php $variable = "check this out ma!"; ?>[/div] joy joy joy [div]<?php $variable
=
"check this out ma!"; ?>[/div] some more useless text';
$output = preg_replace('|\[div](.*?)\[/div]|ise','highlight_string("\\1",true)',$allLines);
echo $output;
?>
outputs:
Code: Select all
<?php ?>
this is a test of the div tagging... <?php = "check this out ma!"; ?> joy joy joy <?php
=
"check this out ma!"; ?> some more useless text
Posted: Tue Jun 08, 2004 8:40 pm
by Steveo31
Awesome! Thanks a truckload feyd.
But one problem- variables in the database are, again, trying to be parsed. I tried escaping but it says theres an "unexpected escape ASCII" or something.
Thanks again man.

Posted: Tue Jun 08, 2004 8:44 pm
by feyd
hmmm so does something like this work?
Code: Select all
<?php
$allLines = 'this is a test of the div tagging... [div]<?php $variable = "check this out ma!"; ?>[/div] joy joy joy [div]<?php $variable
=
"check this out ma!"; ?>[/div] some more useless text';
$output = preg_replace('|\[div](.*?)\[/div]|ise','highlight_string("\\1",true)',addslashes($allLines));
echo $output;
?>
maybe stripslashes($allLines) would work.
Posted: Tue Jun 08, 2004 9:17 pm
by Steveo31
This is the database entry:
Code: Select all
Here is some code:
[div]
<table>
<?php
$stuff = 'Test variable';
?>
</table>
[/div]
And some more.
Code:
Code: Select all
$output = preg_replace('|\[div](.*?)\[/div]|ise','highlight_string("\\1",true)', $row['body']);
echo nl2br($output);
$row['body'] is the mysql fetch_assoc field. No probs there.
Output:
Code: Select all
<table>
<?php
=
Warning: Unexpected character in input: ''' (ASCII=92) state=1 in c:\phpdev\www\projects\multimedia\10 hour\includes\content\articles.php(31) : regexp code on line 7
'Test variable'';
?>
</table>
Posted: Tue Jun 08, 2004 10:00 pm
by feyd
sounds like addslashes or stripslashes would probably do the trick. I'm getting confused..

Posted: Tue Jun 08, 2004 10:08 pm
by markl999
Not sure if this is of any use to you, but this seems to work for me:
Code: Select all
preg_match('|(\[div])(.*?)(\[/div])|is', $row['body'], $matches);
$output = $matches[1].highlight_string($matches[2],true).$matches[3];
echo $output;
Posted: Wed Jun 09, 2004 11:09 am
by Steveo31
feyd wrote:sounds like addslashes or stripslashes would probably do the trick. I'm getting confused..

Yeh.. tried that. I'll keep on it.
Mark- that doesn't seem it would work for multiple entries..