![]() |
![]() |
![]() |
||||
Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact us. |
![]() ![]() |
|
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed. |
|
Thread Tools |
![]() |
#1 |
Too lazy to set a custom title
Industry Role:
Join Date: Aug 2002
Posts: 55,372
|
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?
__________________
Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence. ![]() WP Stuff |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#2 |
Confirmed User
Industry Role:
Join Date: May 2011
Location: San Diego
Posts: 328
|
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/ |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#3 |
It's 42
Industry Role:
Join Date: Jun 2010
Location: Global
Posts: 18,083
|
\"video-title
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#4 |
MFBA
Industry Role:
Join Date: Mar 2003
Location: PNW
Posts: 7,230
|
i know regex well enough to know thats not correct, but i dont know regex well enough to help you either.
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#5 | |
Confirmed User
Join Date: Oct 2002
Posts: 3,745
|
Quote:
/<h1 class="video-title([^>]*)\">([^<]*)<\/h1>/ |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#6 |
Beer Money Baron
Industry Role:
Join Date: Jan 2001
Location: brujah / gmail
Posts: 22,157
|
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 ) ) { // var_dump($m); $h1 = $m[1]; }
__________________
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#7 | |
Too lazy to set a custom title
Industry Role:
Join Date: Aug 2002
Posts: 55,372
|
Quote:
__________________
Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence. ![]() WP Stuff |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#8 |
Beer Money Baron
Industry Role:
Join Date: Jan 2001
Location: brujah / gmail
Posts: 22,157
|
I didn't even think to test fris' regex, but it works in this example too
__________________
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#9 |
Beer Money Baron
Industry Role:
Join Date: Jan 2001
Location: brujah / gmail
Posts: 22,157
|
I want to expand on #2 post, so you can see a working example.
Code:
$html = '<h1 class="video-title one two clearfloat">get this</h1>'; $dom = new DOMDocument(); $dom->loadHTML( $html ); $xpath = new DOMXpath( $dom ); $h1 = $xpath->query( '//h1[contains(@class,"video-title")]' ); var_dump( $h1->item(0)->nodeValue );
__________________
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#10 | |
Too lazy to set a custom title
Industry Role:
Join Date: Aug 2002
Posts: 55,372
|
Quote:
__________________
Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence. ![]() WP Stuff |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#11 |
Beer Money Baron
Industry Role:
Join Date: Jan 2001
Location: brujah / gmail
Posts: 22,157
|
I can believe it, the DOM method does a lot more but is definitely overkill for just a simple one-off regex.
__________________
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#13 |
Confirmed User
Join Date: Apr 2002
Posts: 901
|
![]() also try http://www.rubular.com/
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#14 | |
Confirmed User
Industry Role:
Join Date: Jan 2010
Location: Europe
Posts: 1,327
|
Quote:
__________________
icq: 395 294 346 http://www.adultsubmitter.eu - submit any adult site to 20 directories from 1 form! now 20 domains! http://www.porndeals.eu http://www.ebonybangbros.com |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#15 |
Beer Money Baron
Industry Role:
Join Date: Jan 2001
Location: brujah / gmail
Posts: 22,157
|
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.
__________________
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#16 |
Confirmed User
Join Date: Oct 2002
Posts: 3,745
|
See my post for four reasons that's wrong.
__________________
For historical display only. This information is not current: support@bettercgi.com ICQ 7208627 Strongbox - The next generation in site security Throttlebox - The next generation in bandwidth control Clonebox - Backup and disaster recovery on steroids |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#19 |
Too lazy to set a custom title
Industry Role:
Join Date: Aug 2002
Posts: 55,372
|
Code:
<?php $html = '<h1 class="video-title one two clearfloat">get this</h1>'; $raymor_regex = '/<h1 class="video-title([^>]*)\">([^<]*)<\/h1>/'; $brujah_regex = '/<h1 class="video-title[^"]+">([^<]+)/'; $mikke_regex = '/<h1 class="video-title\s+(.*?)">(.*?)<\/h1>/i'; preg_match($raymor_regex,$html,$raymor); preg_match($brujah_regex,$html,$brujah); preg_match($mikke_regex,$html,$mikke); echo "raymor \n\n"; print_r($raymor); echo "brujah \n\n"; print_r($brujah); echo "mikke \n\n"; print_r($mikke); Code:
raymor Array ( [0] => <h1 class="video-title one two clearfloat">get this</h1> [1] => one two clearfloat [2] => get this ) brujah Array ( [0] => <h1 class="video-title one two clearfloat">get this [1] => get this ) mikke Array ( [0] => <h1 class="video-title one two clearfloat">get this</h1> [1] => one two clearfloat [2] => get this )
__________________
Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence. ![]() WP Stuff |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#20 |
Too lazy to set a custom title
Industry Role:
Join Date: Aug 2002
Posts: 55,372
|
modified brujah's with ending </h1>
Code:
/<h1 class="video-title[^"]+">([^<]+)<\/h1>/ Code:
Array ( [0] => <h1 class="video-title one two clearfloat">get this</h1> [1] => get this )
__________________
Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence. ![]() WP Stuff |
![]() |
![]() ![]() ![]() ![]() ![]() |