Regex pattern for preg_match_all
| From: | Tommy Pham | Date: | Fri, 18 Feb 2011 21:36:30 +0000 |
| Subject: | Regex pattern for preg_match_all | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-311434@lists.php.net to get a copy of this message | ||
Hi folks,
This is not directly relating to PHP but it's Friday so I'm gonna give
it a shot :). Would someone please help me figure out why my regex
pattern doesn't work. Below is the code and sample data:
$html = <<<HTML
<li class="small tab "><a class="y-mast-link images"
href="http://images.search.yahoo.com/images"
data-b="http://www.yahoo.com"><span
class="tab-cover y-mast-bg-hide"
style="padding-left:0em;padding-right:0em;">Images</span></a></li>
<li class="small tab "><a class="y-mast-link video"
href="http://video.search.yahoo.com/video"
data-b="http://www.yahoo.com"><span
class="tab-cover y-mast-bg-hide"
style="padding-left:0em;padding-right:0em;">Video</span></a></li>
<li class="small tab "><a class="y-mast-link local"
href="http://local.yahoo.com/results"
data-b="http://www.yahoo.com"><span
class="tab-cover y-mast-bg-hide"
style="padding-left:0em;padding-right:0em;">Local</span></a></li>
<li class="small tab "><a class="y-mast-link shopping"
href="http://shopping.yahoo.com/search"
data-b="http://www.yahoo.com"><span
class="tab-cover y-mast-bg-hide"
style="padding-left:0em;padding-right:0em;">Shopping</span></a></li>
<li class="small lasttab more-tab "><a class="y-mast-link more"
href="http://tools.search.yahoo.com/about/forsearchers.html"
><span
class="tab-cover y-mast-bg-hide">More</span><span
class="y-fp-pg-controls arrow"></span></a></li>
HTML;
$pattern =
'%<a\s[^href]*href\s*=\s*[\'|"]?([^\'|"|#]+)[\'|"]?\s*[^>]*>(.*)?</a>%im';
preg_match_all($pattern, $html, $matches);
The only matches I got is:
Match 1 of 1: <a class="y-mast-link local"
href="http://local.yahoo.com/results"
data-b="http://www.yahoo.com"><span
class="tab-cover y-mast-bg-hide"
style="padding-left:0em;padding-right:0em;">Local</span></a>
Group 1: http://local.yahoo.com/results
Group 2: <span class="tab-cover y-mast-bg-hide"
style="padding-left:0em;padding-right:0em;">Local</span>
The pattern I made was to work in cases where the page is
non-compliant to any of standard W3.
Thanks,
Tommy