alexvassel
alexvassel

Reputation: 10740

php regex subpatterns

Having some problems with creating regular expression with subpatterns on php. Need some help.

I have such html code:

<div class="result-item *sr "> 
 <a href="/watch?v=_CvG8Eu0nSY" class="ux-thumb-wrap result-item-thumb"><span class="video-thumb ux-thumb-128 "><span class="clip"><img onload="tn_load(1)" alt="Thumbnail" src="//i4.ytimg.com/vi/_CvG8Eu0nSY/default.jpg" ></span></a></div>

So I want to have in $matches[0] - "/watch?v=_CvG8Eu0nSY" and in $matches[1] - "i4.ytimg.com/vi/_CvG8Eu0nSY/default.jpg".

Thanks for your answers!

Upvotes: 0

Views: 386

Answers (2)

vicTROLLA
vicTROLLA

Reputation: 1529

Use DOMDocument. Heres an example:

$html = '<div class="result-item *sr "> 
<a href="/watch?v=gbF_fwTfZ9U" class="ux-thumb-wrap result-item-thumb"><span class="video-thumb ux-thumb-128 "><span class="clip"><img onload="tn_load(10)" alt="Thumbnail" data-thumb="//i4.ytimg.com/vi/gbF_fwTfZ9U/default.jpg" src="//s.ytimg.com/yt/img/pixel-vfl3z5WfW.gif" ></span></a> 
</div>';

$d = new DOMDocument();

$d->loadHTML($html);

$a = $d->getElementsByTagName("a");

foreach($a as $foo) {
        print $foo->getAttributeNode("href")->nodeValue."\n";

        $imgs = $foo->getElementsByTagName("img");
        foreach($imgs as $img) {
                print $img->getAttributeNode("data-thumb")->nodeValue."\n";
        }
}

Upvotes: 2

Mr. Llama
Mr. Llama

Reputation: 20899

The following should work:

preg_match("/a href=\"(\/watch\?v=.*)\".*data-thumb=\"\/\/(.*)\"/U", $html, $matches);

There's probably a more elegant way of pulling it off, but this was the first way that came to mind.

Upvotes: 0

Related Questions