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
https://forums.phpfreaks.com/topic/41240-solved-preg_replace/
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
https://forums.phpfreaks.com/topic/41240-solved-preg_replace/#findComment-199793
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
https://forums.phpfreaks.com/topic/41240-solved-preg_replace/#findComment-200400
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.