Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
-
Burrito
- Spockulator
- Posts: 4715
- Joined: Wed Feb 04, 2004 8:15 pm
- Location: Eden, Utah
Post
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?
-
Burrito
- Spockulator
- Posts: 4715
- Joined: Wed Feb 04, 2004 8:15 pm
- Location: Eden, Utah
Post
by Burrito »
hmm...
I tried that and now it's not matching anything

-
feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Post
by feyd »
new code?

-
Burrito
- Spockulator
- Posts: 4715
- Joined: Wed Feb 04, 2004 8:15 pm
- Location: Eden, Utah
Post
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);

-
bokehman
- Forum Regular
- Posts: 509
- Joined: Wed May 11, 2005 2:33 am
- Location: Alicante (Spain)
Post
by bokehman »
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);
-
feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Post
by feyd »
The e modifier is, unfortunately a security hole waiting to be exploited.
-
bokehman
- Forum Regular
- Posts: 509
- Joined: Wed May 11, 2005 2:33 am
- Location: Alicante (Spain)
Post
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.