php - Take what's between [code][/code] and apply changes -


i want make code box, can apply changes. if have this: $var= "word"; inside these [code] here [/code], change $var red color , "word" green.

i used preg_replace select what's between these [code] [/code].

$codebox = preg_replace("/\[code\](.*?)\[\/code\]/","$1",$string); 

the thing preg_replace can outsider changes (to whole code). want changes inside these [code] [/code]. like: background color, text color, text font, text font-weight , etc... that's mean need out apply changes put back.

i want able use str_replace , preg_replace functions on $1 not on $string.

for example change"word" green. i'll use

preg_replace("/(\".*?\")/","<span style='color: #090;'>$1</span>",$string) 

and can't use preg_replace inside preg_replace, can i? don't know if i'm using wrong function here, or there way this.

you may find patterns wrong, correct me learnt them yesterday.

using preg_replace_callback:

$string = '[code]$var = "word";[/code]'; $codebox = preg_replace_callback("/\[code\](.*?)\[\/code\]/",function($m){     // following replacements demo     $m[1] = preg_replace('/"([^"]+)"/', '"<span style="color:#0d0;">$1</span>"', $m[1]); // green value     $m[1] = preg_replace('/(\$\w+)/', '<span style="color:#f00;">$1</span>', $m[1]); // red var name     $m[1] = str_replace(' = ', '<span style="color:#00f;"> = </span>', $m[1]); // blue = sign     return $m[1]; },$string); echo $codebox; 

online demo.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -