1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Any Regular Expression geniuses?

Discussion in 'PHP' started by adbox, Aug 31, 2010.

  1. #1
    I have this blob of text
    
                <div class="yn-story-content">
                    <p>CLEVELAND &ndash; A federal judge in Ohio has ordered mental health treatment for the owner of a bear that fatally mauled its caretaker.</p>
                    <p>A magistrate judge signed an order Friday in Cleveland tightening probation requirements for Sam Mazzola.</p>
                    <p>The type of treatment wasn't outlined in court filings, and Mazzola's attorney didn't immediately return a message Monday seeking comment.</p>
                    <p>One of Mazzola's bears attacked and killed the caretaker Aug. 19. Mazzola keeps bears, wolves, tigers and a lion at a compound in Columbia Station southwest of Cleveland.</p>
    
                    <p>Mazzola got probation after pleading guilty last year to transporting a black bear without a license and selling a skunk without a license.</p>
                    <p></p>
                </div>
    Code (markup):
    and this regex:
    
    /\<div class\="yn-story-content"\>(.*?)\<\/div\>/
    Code (markup):
    and what it should do is grab the content between the two divs but it doesnt. I'd really like to know why? Any help?
     
    adbox, Aug 31, 2010 IP
  2. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $subject = <<< ENDLINE
    <div class="yn-story-content">
                    <p>CLEVELAND &ndash; A federal judge in Ohio has ordered mental health treatment for the owner of a bear that fatally mauled its caretaker.</p>
                    <p>A magistrate judge signed an order Friday in Cleveland tightening probation requirements for Sam Mazzola.</p>
                    <p>The type of treatment wasn't outlined in court filings, and Mazzola's attorney didn't immediately return a message Monday seeking comment.</p>
                    <p>One of Mazzola's bears attacked and killed the caretaker Aug. 19. Mazzola keeps bears, wolves, tigers and a lion at a compound in Columbia Station southwest of Cleveland.</p>
    
                    <p>Mazzola got probation after pleading guilty last year to transporting a black bear without a license and selling a skunk without a license.</p>
                    <p></p>
                </div>
                
    ENDLINE;
    
    $found = '';
    
    if (preg_match('/\<div class\="yn\-story\-content"\>(.*)\<\/div\>/siU', $subject, $matches))
    {
    	$found = $matches[1];
    }
    
    if ($found)
    {
    	echo $found;
    }
    else
    {
    	echo 'Not found';
    }
    PHP:
    The regex modifiers are siU

    s - span multiple lines
    i - case insensitive
    U - ungreedy
     
    exam, Aug 31, 2010 IP
  3. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #3
    The problem is you need the s modifier.
     
    danx10, Aug 31, 2010 IP
  4. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #4
    Hello Exzam and danx10,

    I applied the recommendation above and it is not respecting the ungreedy modifier for this example:

    Blob:
    <link>http://www.articlecity.com/articles/health/</link><link>http://www.articlecity.com/articles/health/article_6896.shtml</link><link>http://www.articlecity.com/articles/health/article_7858.shtml</link><link>http://www.articlecity.com/articles/health/3.shtml</link>
    Code (markup):
    Regex:

    /\<link\>(.*?)\<\/link\>/siU
    Code (markup):
    What should be the first result:

    http://www.articlecity.com/articles/health/
    Code (markup):
    Instead this is the result:

    http://www.articlecity.com/articles/health/</link><link>http://www.articlecity.com/articles/health/article_6896.shtml</link><link>http://www.articlecity.com/articles/health/article_7858.shtml</link><link>http://www.articlecity.com/articles/health/3.shtml
    Code (markup):

    Originally this regex was taking care of this propperly, but adding on the /siU seemed to reverse the ungreedy of my original regex below. Maybe its the ? :

    /$start(.*?)$end/
    Code (markup):
     
    adbox, Aug 31, 2010 IP
  5. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #5
    Any idea guys?
     
    adbox, Sep 1, 2010 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    .*? the ? makes it ungreedy you either use that or the U modifier it makes no sense to use both, furthermore use <pre> if neccessary (when viewing the matches).
     
    danx10, Sep 1, 2010 IP