preg_replace match, works with string functions?

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
semlar
Forum Commoner
Posts: 61
Joined: Fri Feb 20, 2009 10:45 pm

Re: preg_replace match, works with string functions?

Post by semlar »

I think you need to use preg_replace_callback if you want to use a function to handle the replacement.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: preg_replace match, works with string functions?

Post by Chris Corbyn »

Also, the reason your first version didn't work was because you need to wrap the replacement code inside a string so that PHP can eval() it.

Code: Select all

$test = 'Hello <B>THERE<I>';
$test = preg_replace('/.*?\.*?/e', "return strtolower('\\1');", $test);
I'm not 100% but you may need to use $1 in this specific scenario (where you're using the "e" modifier) too. I seem to remember that being the case, but logically I can't see why.
Post Reply