Jump to content

preg_replace with highlight_string


Roee

Recommended Posts

hi all,
in the last days, i have been tring to make a system with BBCODE.. i want to make the content of [mycode] and [/mycode], in color in php.

i want to make it in preg_replace(there is no another way..)

i write this code:

[code]
$content = preg_replace( "#\[mycode\](.+?)\[/mycode\]#is", "XXXXXXXXX", $content );  
[/code]

but i have no idea what to write in XXXXXXXXX...
how i use in highlight_string ??

thanks
Link to comment
Share on other sites

  • 4 weeks later...
and thats not working ...

heres what that script outputs:

'
'.highlight_string(stripslashes('<html>......................

highlight_string is included in the [b]output[/b], so not executed so not doing what it has to do!
Link to comment
Share on other sites

You'll want to use preg_replace_callback instead if you want to call a function when using regular expressions like so:

[code]<?php

function highlight_php($matches)
{
    $php = highlight_string($matches[0], true);

    //strip out the phptags
    $php = preg_replace("#(\[php\]|\[/php\])#i", "", $php);

    return $php;
}

$str = "[php]<?php

echo \"hello world\";

?>[/php]";

$match = "#\[php\](.*?)\[\/php\]#is";
$php = preg_replace_callback($match, 'highlight_php', $str);

echo $php;
?>[/code]
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.