Jump to content

[SOLVED] preg_replace


WorldDrknss

Recommended Posts

I am having an issue with preg_replace while trying to use php highlight_string.

I have tried the following:

$string = preg_replace("/\<span class=\"PHP\">(.+?)\<\/span>/s", "\"<table border=\"0\" cellspacing=\"1\" cellpadding=\"3\" width=\"90%\" align=\"center\"><tr><td><strong>PHP:</strong></td></tr><tr><td class=\"PHP\">\".highlight_string('\\1').\"</td></tr></table>"", $string);

 

$string = preg_replace("/\<span class=\"PHP\">(.+?)\<\/span>/s", "<table border=\"0\" cellspacing=\"1\" cellpadding=\"3\" width=\"90%\" align=\"center\"><tr><td><strong>PHP:</strong></td></tr><tr><td class=\"PHP\">.highlight_string('\\1').</td></tr></table>", $string);

 

$string = preg_replace("/\<span class=\"PHP\">(.+?)\<\/span>/s", "<table border=\"0\" cellspacing=\"1\" cellpadding=\"3\" width=\"90%\" align=\"center\"><tr><td><strong>PHP:</strong></td></tr><tr><td class=\"PHP\">".highlight_string('\\1')."</td></tr></table>", $string);

 

I just can't seem to figure this one out.

Any help appreciated,

Thanks.

Link to comment
Share on other sites

Which part of the code would you like besides the three I posted before?

Here this function if that helps:

function code_replace($text){
	$string = $text;
	$string = preg_replace("/\<span class=\"Code\">(.+?)\<\/span>/s", "<table border=\"0\" cellspacing=\"1\" cellpadding=\"3\" width=\"90%\" align=\"center\"><tr><td><strong>Code:</strong></td></tr><tr><td class=\"Code\">\\1</td></tr></table>", $string);
	$string = preg_replace("/\<span class=\"Quote\">(.+?)\<\/span>/s", "<table border=\"0\" cellspacing=\"1\" cellpadding=\"3\" width=\"90%\" align=\"center\"><tr><td><strong>Quote:</strong></td></tr><tr><td class=\"Code\">\\1</td></tr></table>", $string);
	//$string = preg_replace("/\<span class=\"PHP\">(.+?)\<\/span>/s", "<table border=\"0\" cellspacing=\"1\" cellpadding=\"3\" width=\"90%\" align=\"center\"><tr><td><strong>PHP:</strong></td></tr><tr><td class=\"PHP\">.highlight_string('\\1').</td></tr></table>", $string);
	return $string;
}

 

obviously the one that is commented out is the one I am currently seeking help for as the first two work.

Link to comment
Share on other sites

I tried the e modifier but returned a fatal error, but I found an alternative and used preg_replace_callback.

 

function code_box($matches){
	return "<table border=\"0\" cellspacing=\"1\" cellpadding=\"3\" width=\"90%\" align=\"center\"><tr><td><strong>PHP:</strong></td></tr><tr><td class=\"PHP\">".highlight_string(html_entity_decode($matches[1], ENT_QUOTES), TRUE)."</td></tr></table>";
}

 

$string = preg_replace_callback("/\<span class=\"PHP\">(.+?)\<\/span>/s", "code_box", $string);

 

Thanks for your help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.