0

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!

3
  • 1
    Let's see your attempts, then. Commented Mar 15, 2011 at 19:55
  • BTW scraping from the web is considered bad form. Collect your own content. Commented Mar 15, 2011 at 19:56
  • Bad idea to change the example in the question, making the answers to look odd... Also bad idea to use regexes on HTML/XML... Commented Mar 15, 2011 at 20:18

2 Answers 2

2

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";
        }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

You probably want .*?s, in case there are several tags.
Aye, that's why I threw on an Ungreedy flag at the end. I'm kinda lazy when it comes to modifiers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.