WorldDrknss Posted March 5, 2007 Share Posted March 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 5, 2007 Share Posted March 5, 2007 I dont see the code please show it ok. Quote Link to comment Share on other sites More sharing options...
WorldDrknss Posted March 5, 2007 Author Share Posted March 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
effigy Posted March 5, 2007 Share Posted March 5, 2007 See Example 1647. You need the /e modifier. Quote Link to comment Share on other sites More sharing options...
WorldDrknss Posted March 6, 2007 Author Share Posted March 6, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.