ChadNomad Posted May 8, 2008 Share Posted May 8, 2008 I have some text stored in a database like this: Some content [phpcode]<?php echo $test; ?>[/phpcode] some content [phpcode]<?php echo $test; ?>[/phpcode] I can't thing of a logical way to get the code and use the highlight_string function.... Confused! Help appreciated, thanks! Quote Link to comment Share on other sites More sharing options...
thebadbad Posted May 8, 2008 Share Posted May 8, 2008 <?php $text = 'Some content [phpcode]<?php echo $test; ?>[/phpcode] some content [phpcode]<?php echo $test; ?>[/phpcode]'; $text = preg_replace('/\[phpcode\](.*?)\[\/phpcode\]/ie', 'highlight_string(\'$1\', TRUE)', $text); echo $text; ?> Notes: The e modifier at the end of the pattern parameter makes preg_replace() treat the replacement parameter as PHP code after the appropriate references substitution is done (i.e. after $1 is substituted with the parenthesized content of the pattern parameter). Also, if the content from the database contains single quotes, they should be escaped, or the code will fail. Quote Link to comment Share on other sites More sharing options...
psychowolvesbane Posted May 8, 2008 Share Posted May 8, 2008 <?php $text = 'Some content;?> [phpcode]<?php echo $test; ?>[/phpcode] some content [phpcode]<?php echo $test; ?>[/phpcode] <?php $text = preg_replace('/\[phpcode\](.*?)\[\/phpcode\]/ie', 'highlight_string(\'$1\', TRUE)', $text); echo $text; ?> 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.