[SOLVED]highlight_string, but not all?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

[SOLVED]highlight_string, but not all?

Post 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 :wink:
Last edited by Steveo31 on Thu Dec 02, 2004 1:22 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I've done this before (untested code):

Code: Select all

preg_replace('|\[div](.*?)\[/div]|ie','highlight_string("\\1",true)',$allLines);
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

AAAAH!!! Man, that preg_replace code is killin me!!


I'll try that out when my blood pressure gets below 200 ;)
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

\\1 is the same as \$1 last I checked.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the way that codes set it'll replace everything in the body with the div if [div and [/div exist.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post 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. :D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post 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>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

sounds like addslashes or stripslashes would probably do the trick. I'm getting confused.. :)
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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;
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post 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..
Post Reply