I have this blob of text <div class="yn-story-content"> <p>CLEVELAND – 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?
$subject = <<< ENDLINE <div class="yn-story-content"> <p>CLEVELAND – 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
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):
.*? 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).