preg_replace troubles

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Anman
Forum Newbie
Posts: 1
Joined: Tue Oct 20, 2009 5:28 pm

preg_replace troubles

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: preg_replace troubles

Post 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);
Post Reply