I want to make code box, where I can apply changes. If I have this: $var= "word";
inside these [code]
HERE [/code]
, I will change $var
into red color and "word"
into green.
I used preg_replace
to select what's between these [code]
[/code]
.
$codebox = preg_replace("/\[code\](.*?)\[\/code\]/","$1",$string);
the thing is with preg_replace I can do outsider changes (to whole code). I want to do changes to what inside these [code]
[/code]
. Like: background color, all text color, all text font, all text font-weight and etc... that's mean I need to get it out apply changes then put it back.
I want to be able to use str_replace
and preg_replace
functions on $1
not on $string
.
for example to change"word"
into green. I'll use
preg_replace("/(\".*?\")/","<span style='color: #090;'>$1</span>",$string)
and I can't use preg_replace
inside preg_replace
, can I?
I don't know if I'm using the wrong function here, or there is a way do this.
you may find my patterns wrong, correct me I just learnt them yesterday.
Using preg_replace_callback:
Online demo.
Targeting the code inside the tags is a bit simpler when you extract it first using
preg_match
instead ofpreg_replace
:Your replaces return strings, so they can be nested just fine.
Also, I suggest looking at a text to regex converter if you're new to regular expressions. It helped me get a grasp on them better:
http://txt2re.com/index-php.php3?s=$bacon%20=%20%22good%22;&-21&2&6&-23&7&1&-22