Page 1 of 1

when does regex evaluate matches?

Posted: Thu Aug 03, 2006 1:06 pm
by Burrito
I have this:

Code: Select all

$string = preg_replace('/<strong class="red">(.*?)<\/strong>/i','<strong class="red">'.strtolower("\\1").'</strong>',$string);
and it finds all my matches just fine, but it's not running strtolower() on my matches for my replacements???

any ideas why this might be or more importantly, how I can make it work?

Posted: Thu Aug 03, 2006 1:13 pm
by feyd
strtolower() is run first followed by the concatenation. After which preg_replace() is called. You want to use preg_replace_callback().

Posted: Thu Aug 03, 2006 1:16 pm
by Burrito
hmm...

I tried that and now it's not matching anything :?:

Posted: Thu Aug 03, 2006 1:23 pm
by feyd
new code? Image

Posted: Thu Aug 03, 2006 1:34 pm
by Burrito
nm...I got it working with this:

Code: Select all

function back($matches)
{
	return "<strong class=\"red\">".ucwords(strtolower($matches[1]))."</strong>";
}
$string = preg_replace_callback('/<strong class="red">(.*?)<\/strong>/i',"back"),$string);
Image

Posted: Thu Aug 03, 2006 6:42 pm
by bokehman
feyd wrote:strtolower() is run first followed by the concatenation. After which preg_replace() is called. You want to use preg_replace_callback().
preg_replace could be used if used in conjunction with the "e" modifier.

Code: Select all

$string = preg_replace('/(?<=<strong class="red">).*?(?=<\/strong>)/ie', "strtolower('$0')", $string);

Posted: Thu Aug 03, 2006 8:02 pm
by feyd
The e modifier is, unfortunately a security hole waiting to be exploited.

Posted: Thu Aug 03, 2006 8:46 pm
by bokehman
feyd wrote:The e modifier is, unfortunately a security hole waiting to be exploited.
It's very fussy too... I think I like your method better.