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! Link to comment https://forums.phpfreaks.com/topic/104736-formatting-code/ 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. Link to comment https://forums.phpfreaks.com/topic/104736-formatting-code/#findComment-536139 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; ?> Link to comment https://forums.phpfreaks.com/topic/104736-formatting-code/#findComment-536216 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.