Nesting preg_replace functions while escaping tokens

TrernLync

New Member
I'm using the 'e' modifier in preg_replace to search for a given string. Then I need it to look within that string for individual matches using another preg_replace statement:\[code\]$str = "This is FOO.";$str = preg_replace('/(foo)/ie', "x$1", $str);echo $str;\[/code\]This will generate: \[code\]This is xFOO.\[/code\]
Now I need to nest. (I'm nesting because I need to match an unknown number of groups into separate tokens which is not possible with a single preg_replace statement as discussed elsewhere on this forum. I've obviously simplified this code to focus on my question).\[code\]$str = "This is FOO.";$str = preg_replace('/(foo)/ie', "preg_replace('/(\w)/i','x$1','$1')", $str);echo $str;\[/code\]I need this to generate \[code\]This is xFxOxO.\[/code\]
But instead it generates \[code\]This is xFOOxFOOxFOO.\[/code\]When php evaluates the second line of code, it's replacing both $1 tokens with the matched string \[code\]foo\[/code\]. I need it to ignore the first $1 token because that is supposed to be evaluated by the inner preg_replace statement. How can I escape the first $1 token so that it won't be evaluated by the outside preg_replace statement?
 
Back
Top