![]() |
regex to match a class element
im looking to match a h1 tag with a class, but also match after that class so
<h1 class="video-title one two clearfloat">get this</h1> /<h1 class="video-title .*?\">(.*?)<\/h1>/ is this correct? |
i suck at remembering regex, so i always use dom->xpath queries instead. you could find the tag with a query something like this:
<?php $dom = new DOMDocument(); $dom->loadHTML(HTMLHERE); $xpath = new DOMXpath($dom); $selectH1 = $xpath->query('//h1[@class="classnamehere"]'); ?> just came up with that right now, so dunno if that's the exact syntax, but i prefer traversing html w/ dom rather than regex :) this is a good xpath query guide: http://www.earthinfo.org/xpaths-with-php-by-example/ |
\"video-title
|
i know regex well enough to know thats not correct, but i dont know regex well enough to help you either. :( sorry
|
Quote:
/<h1 class="video-title([^>]*)\">([^<]*)<\/h1>/ |
Like post #2 I prefer to use DOM for this type thing but something else I tend to do is use negatives in my regex so like this where $html contains the html/string:
Code:
if ( preg_match( '/<h1 class="video-title[^"]+">([^<]+)/', $html, $m ) ) |
Quote:
|
I didn't even think to test fris' regex, but it works in this example too
|
I want to expand on #2 post, so you can see a working example.
Code:
|
Quote:
|
Quote:
|
ya only 1 regex was needed
|
also try http://www.rubular.com/
|
Quote:
|
These regex examples also rely on video-title to be the first class immediately after the quote mark, the DOM example is smart enough that it doesn't matter where video-title is located in the class order.
|
Quote:
|
Quote:
|
Quote:
|
Code:
<?php Code:
|
modified brujah's with ending </h1>
Code:
/<h1 class="video-title[^"]+">([^<]+)<\/h1>/ Code:
Array |
All times are GMT -7. The time now is 03:26 PM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123