Results 1 to 3 of 3

Thread: Another RegEx

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default Another RegEx

    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

    PHP Code:
    $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

    PHP Code:
    preg_match_all('/<ol.*?>(.*?)<\/ol>/'$content$matchesPREG_SET_ORDER); 
    and

    PHP Code:
    preg_replace_callback('/<ol.*?>(.*?)<\/ol>'"html_list"$content); 
    neither of which find the ordered list.

    The ordered list is

    Code:
    <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.
    Corrections to my coding/thoughts welcome.

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    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.r...onditional.php

    Those are my two guesses as to what you are looking for.
    To choose the lesser of two evils is still to choose evil. My personal site

  3. The Following User Says Thank You to james438 For This Useful Post:

    bluewalrus (08-10-2010)

  4. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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.
    Corrections to my coding/thoughts welcome.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •