tryingtolearn Posted September 13, 2009 Share Posted September 13, 2009 I have to extract info from two different type of paragraph tags on a page The first is <p><a href="link.html">text</a></p> The second is <p class="row"> <span class="hh" id="rag"> </span> <a href="link.html">text -</a></p> using <[p^>]+> gets the first kind but Im having problems matching the tag with an attribute I need to get everything inbetween the paragraph tags from both styles. any help would be appreciated Link to comment https://forums.phpfreaks.com/topic/174084-matching-a-paragraph-tag-with-attributes/ Share on other sites More sharing options...
Garethp Posted September 13, 2009 Share Posted September 13, 2009 '~<p[^>]>(.+)</p>~' Link to comment https://forums.phpfreaks.com/topic/174084-matching-a-paragraph-tag-with-attributes/#findComment-917658 Share on other sites More sharing options...
tryingtolearn Posted September 13, 2009 Author Share Posted September 13, 2009 Thanks, still doesnt seem to match it though. Ill keep looking. Link to comment https://forums.phpfreaks.com/topic/174084-matching-a-paragraph-tag-with-attributes/#findComment-917710 Share on other sites More sharing options...
Garethp Posted September 13, 2009 Share Posted September 13, 2009 '~<p[^>]+>(.+)</p>s~' Sorry, I forgot to add the plus Link to comment https://forums.phpfreaks.com/topic/174084-matching-a-paragraph-tag-with-attributes/#findComment-917716 Share on other sites More sharing options...
thebadbad Posted September 14, 2009 Share Posted September 14, 2009 Garethp, <p[^>]+> will not match <p>, you would have to use an asterisk instead of the plus. And you have to make your second quantifier lazy, else it'll match to the last closing tag in the source. Lastly, you misplaced the pattern modifier This'll do: ~<p\b[^>]*>(.*?)</p>~is I added the word boundary \b so that it doesn't wrongfully match tags like pre and param. Important in case those tags appear in the source. Link to comment https://forums.phpfreaks.com/topic/174084-matching-a-paragraph-tag-with-attributes/#findComment-918408 Share on other sites More sharing options...
Garethp Posted September 14, 2009 Share Posted September 14, 2009 Hahah, thanks. How can I make patterns lazy and greedy? I'm not entirely sure how to do that. Link to comment https://forums.phpfreaks.com/topic/174084-matching-a-paragraph-tag-with-attributes/#findComment-918419 Share on other sites More sharing options...
thebadbad Posted September 14, 2009 Share Posted September 14, 2009 A question mark following a quantifier makes it lazy, meaning that it will match as little as possible. There's a longer explanation and some examples in this thread: http://www.phpfreaks.com/forums/index.php/topic,236933.msg1104789.html#msg1104789 Link to comment https://forums.phpfreaks.com/topic/174084-matching-a-paragraph-tag-with-attributes/#findComment-918468 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.