View Full Version : A highlight_string code that works?

11-28-2004, 06:43 AM
Hey all... I'm having one heckuva time getting my php code like tags working:

$output = preg_replace('|\(.*?)\|ise',"\"<div class='div_code'>\".highlight_string('\\\\1',true).\"</div>\"", $row['body']);

I can get it to work like so:

$body =
Some non code.

echo \"Code!!\";

And some more...<stuff>

$output = preg_replace('|\(.*?)\|ise', "highlight_string(\"\\1\", true)", $body);
echo $body;
echo "<div>";
echo $output;
echo "</div>";
But I can't when I pull the code outta the DB. Any clean way of doing this?

11-28-2004, 10:42 AM
Depends on what your error is.
My guess is, you are looking at outputting some tagged references as well correct. Others like say <p>This is the greatest thing in the world</p> etc? If your doing stuff like that, and its working fin for what you put in manually, I'd say off hand it probably is relating to the way your quotation and double quotations are. Try using addslashes or stripslashes accordingly to see if it solves your problem.

You have your $row['body'] in your first section, but not the lower, so I'm assuming thats for your sql output.
But without seeing your problems/errors and more code / output, sorry I can only guess as to what the root problem is.

You don't need the ? in the replacment pattern here: (.*?) that will allow it to have any number of characters any number of times. So you don't need to add a zero or one modifier.

12-01-2004, 11:10 PM
Ok, finally got it.

$output = preg_replace('|\(.*)\|ise',"\"<div class='div_code'>\".highlight_string(stripslashes('\\1'),true).\"</div>\"", $row['body']);

Just change the "div" to "php".

12-01-2004, 11:23 PM
Here's something much better than the php highlighter: http://mandala.co.uk/syntax_highlighting/. You might want to change the colors to something that's better for the eyes.