jwwceo Posted December 3, 2009 Share Posted December 3, 2009 Hello, I have a piece of code which contains the following: <h1 class="productTitle"> Solar grid</h1> <div class="image"> <img src="../images/IGT.jpg" alt="Solar grid" /> </div> <div class="story"> XXX A BUNCH OF PLAIN TEXT XXX </div> and I am trying to parse a bunch of similar pages: I have the following preg_match code: $title = '/class="productTitle">(.+?)<\/h1>/'; $description = '/class="story">(.+?)<\/div>/'; preg_match($title,$source,$match1); preg_match($description,$source,$match2); The two search terms are identical, but only the top preg_match is returning a value in $match1, $match2 is empty. Is there something about this preg_match code I am missing?? Is is because there are other </div>'s on the page and not just one, like there are with the h1 tag?? Any help would be awesome James Link to comment https://forums.phpfreaks.com/topic/183889-preg-_match-is-there-something-i-am-doing-wrong/ Share on other sites More sharing options...
nrg_alpha Posted December 3, 2009 Share Posted December 3, 2009 Your second pattern lacks the s modifier, so (.+?) will not get far, as it will not include newlines. I suspect this is something along the lines you are after: Example: $source = <<<EOF <h1 class="productTitle"> Solar grid</h1> <div class="image"> <img src="../images/IGT.jpg" alt="Solar grid" /> <> <div class="story"> XXX A BUNCH OF PLAIN TEXT XXX <> EOF; $title = '#class="productTitle">(.+?)</h1>#'; $description = '#class="story">(.+?)<>#s'; preg_match($title,$source,$match1); echo $match1[1] . "<br />\n"; preg_match($description,$source,$match2); echo $match2[1]; Granted, I think it would be better to use DOM for this sort of thing (regex can be easily 'fooled' if the pattern isn't well structured). EDIT - Seems like the closing tags in the snippet are not displaying properly in the snippet post.. Will see if this is a reproducable bug and if so, will notify the staff about it. Link to comment https://forums.phpfreaks.com/topic/183889-preg-_match-is-there-something-i-am-doing-wrong/#findComment-970785 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.