Jump to content

Formatting Code?


ChadNomad

Recommended Posts

<?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

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.