Jump to content


Photo

preg_replace with highlight_string


  • Please log in to reply
5 replies to this topic

#1 Roee

Roee
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 29 April 2006 - 05:47 PM

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:

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

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

thanks

#2 LiLaaron

LiLaaron
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 30 April 2006 - 03:40 AM

this may help

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

Aaron

#3 Roee

Roee
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 30 April 2006 - 11:40 AM

this is not help..

#4 Brandon Jaeger

Brandon Jaeger
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts
  • LocationPA
  • Age:25

Posted 30 April 2006 - 10:51 PM

[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]
PHP is my friend (=

#5 stino

stino
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 23 May 2006 - 12:33 PM

and thats not working ...

heres what that script outputs:

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

highlight_string is included in the output, so not executed so not doing what it has to do!

#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 23 May 2006 - 03:40 PM

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

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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users