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
https://forums.phpfreaks.com/topic/8717-preg_replace-with-highlight_string/
Share on other sites

  • 4 weeks later...
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]

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.