Jump to content

Archived

This topic is now archived and is closed to further replies.

Roee

preg_replace with highlight_string

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

Share this post


Link to post
Share on other sites
this may help

[a href=\"http://us3.php.net/highlight_string\" target=\"_blank\"]http://us3.php.net/highlight_string[/a]

Aaron

Share this post


Link to post
Share on other sites
[a href=\"http://us3.php.net/manual/en/function.highlight-string.php#49506\" target=\"_blank\"]http://us3.php.net/manual/en/function.high...tring.php#49506[/a]

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.