Page 1 of 1

Re: preg_replace match, works with string functions?

Posted: Fri Mar 06, 2009 5:12 pm
by semlar
I think you need to use preg_replace_callback if you want to use a function to handle the replacement.

Re: preg_replace match, works with string functions?

Posted: Sat Mar 07, 2009 5:15 am
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.