Forum Moderators: coopster

Message Too Old, No Replies

trying to exclude content with regex wildcard

regex will kill me!

         

doodlebee

9:24 pm on Feb 4, 2008 (gmt 0)

10+ Year Member



Okay, I'm trying to create a small function that will search my WordPress content, and replace certain items. What I'm looking to do is find any instance of the STRONG tag that appears immediately before a UL tag. If it's found, I want to add a class to the STRONG tag.

I've done something similar before, but this is the first time I've ever needed to find something that also had content between the tags. What I'd like to do is have the search/replace ignore searching for the content between STRONG tags, and only add the class to the opening one.

So, here's is a working example, replacing a closing UL tag:


function fix_unordered_end ($content) {
$ul_end = "</ul>";
$rep_end = "</ul>" . "\n" . "<div class=\"innerbottom\"></div>" . "\n" . "<div class=\"bottom\"></div>" . "\n" . "</div>";
$content = str_replace($ul_end, $rep_end, $content);
return ($content);
}


add_filter('the_content', 'fix_unordered_end');

Like I said, this works like a charm. But what I *can't* get to work is that stuff between my STRONG tags. Right now, I'm at this point:


function fix_unordered_title ($content) {
$any = "(.+)";
$find = "<strong>$any</strong></p>" . "\n" . "<ul>";
$replace = "<strong class=\"title\">$1 hey</strong></p>" . "\n" . "<ul>";
$content = str_replace($find, $replace, $content);
return ($content);
}


add_filter('the_content', 'fix_unordered_title');

I've tried MANY variations on this theme - I can find the tags just fine, but when I try to "set aside" the text between my tags, nothing happens. So I know the rest is right - but my "$any" is what's wrong. I've tried what you see, as well as "/(.+)", "/(\w+)" and all kinds of other stuff.

There could be *anything* between the STRONG tags - I just can't seem to get my wildcard right. Can anyone help me figure this out? It's driving me nuts.

PHP_Chimp

2:39 pm on Feb 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



function fix_unordered_title ($content) {
$any = "(.+)";
$find = "<strong>$any</strong></p>" . "\n" . "<ul>";
$replace = "<strong class=\"title\">$1 hey</strong></p>" . "\n" . "<ul>";
$content = str_replace($find, $replace, $content);
return ($content);
}

str_replace doesnt take a regular expression. Try preg_replace [uk3.php.net] and see how that works for you.

You may want to use something like -


$find = '%<strong>(.+)</strong></p>\n<ul>%is';

doodlebee

3:11 pm on Feb 5, 2008 (gmt 0)

10+ Year Member



Thank you!

*excellent*!

This is what worked:

$find = "%<strong>(.+)<\/strong><\/p>" . "\n" . "<ul>%";

You are my HERO - thank you soooo much!