Log in

View Full Version : Another RegEx



bluewalrus
08-10-2010, 01:44 AM
So I thought I was getting a hang onto writing regular expressions but either I'm wrong or missing something.

I have this for my regular expression


<ol.*?>(.*?)<\/ol>

as this


$content = preg_replace('/<ol.*?>(.*?)<\/ol>/', '<div class="ol">$1</div>' . "<br />\n", $content);

I get my expected result but I can't use this function. I need to know how much content there is (how manye line items there are) inside that ol.

I tried


preg_match_all('/<ol.*?>(.*?)<\/ol>/', $content, $matches, PREG_SET_ORDER);


and



preg_replace_callback('/<ol.*?>(.*?)<\/ol>', "html_list", $content);


neither of which find the ordered list.

The ordered list is



<ol>
<li>Ordered List Item 1</li>
<li>Ordered List Item 2</li>
<li>Ordered List Item 3</li>
<li>Ordered List Item 4 5&mu;L/mm 37&deg;C.</li>
</ol>


The fourth item I'm parsing into the incorrect º and µ if that makes a difference. So does anyone see anything I'm doing wrong or know of a difference preg_replace_callback, preg_match_all, and just preg_replace? Thanks.

james438
08-10-2010, 07:02 AM
What is this part of your expression for?
<ol.*?>(.*?)<\/ol>
also you should use the s modifier so your . can work through newlines as in your example otherwise
$content = preg_replace('/<ol.*?>(.*?)<\/ol>/', '<div class="ol">$1</div>' . "<br />\n", $content);
won't work.

As far as the rest of your post I am a little unsure what you are trying to do. Could you elaborate a little more and reword some of what you wrote?


The fourth item I'm parsing into the incorrect º and µ if that makes a difference. So does anyone see anything I'm doing wrong or know of a difference preg_replace_callback, preg_match_all, and just preg_replace? Thanks.

Sorry, I am not sure what you are asking for here. It might also be late and I just need to look at what you wrote after a little sleep too.

If all you want to do is count the number of <li> in your $content when it is between <ol> tags you could use preg_match_all and then use php to count the number of matches.

Or are you trying to do a conditional replacement of terms. see http://us.php.net/manual/en/regexp.reference.conditional.php

Those are my two guesses as to what you are looking for.

bluewalrus
08-10-2010, 01:09 PM
Thanks for the /s hint, seems to be working now. The .*? is to catch any class, ids, or start tags that maybe present. I have also noticed the random text by the bots. I think they may have realized just posting a link is much easier to detect as spam and have set up arrays with random text to fill in around the links.