Jump to content

Formatting Code?


ChadNomad

Recommended Posts

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
Share on other sites

<?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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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