Page 1 of 1

preg_replace troubles

Posted: Tue Oct 20, 2009 5:32 pm
by Anman
Hey, I'm trying to create a syntax highlighter plugin using GeSHi.

My only problem is this:

Code: Select all

 preg_replace('#\[code=(.*?)\](.*?)\[/code\]#i', "<div class='codeblock'>" . geshify("$1", "$2") . "</div>", $message); 
In particular:

Code: Select all

geshify("$1", "$2")
The problem is it's passing literally dollar sign 1 and 2 (respectively) to the function, not the replacement.

I would greatly appreciate any help regarding this matter.

Re: preg_replace troubles

Posted: Tue Oct 20, 2009 5:58 pm
by John Cartwright
Try using preg_replace_callback instead, otherwise the geshify() is evaluated before preg_replace() is run.

Code: Select all

 
function myGeshify($matches) {
   return "<div class='codeblock'>" . geshify($matches[1], $matches[2]) . "</div>";   
}
 
preg_replace_callback('#\[code=(.*?)\](.*?)\[/code\]#i', 'myGeshify', $message);