Anders
Anders

Reputation: 521

How to get all iframes src from html code in PHP

Was just wondering if any of you guys know how to get all iframes src from html code in php? Would help me a lot!

I have tried this so far:

$dom = new DOMDocument();
$html = $data;

@$dom->loadHTML($html);

$a = $dom->getElementsByTagName('iframe');

for ($i; $i < $a->length; $i++) {
$attr = $a->item($i)->getAttribute('src');

echo $attr . "\n";
}

This is the URL: http://www.youtube.com/all_comments?v=wb1u5CeMhkI

And then I want to get the iframe src in the 'comments-iframe-container' div. Search for the div, and the iframe will be 2 lines below the div.

Have a lovely christmas! :)

Upvotes: 3

Views: 2292

Answers (1)

frederickf
frederickf

Reputation: 1531

The iframe you're trying to access is created dynamically via JavaScript. It doesn't exist when you try to load the html with PHP. Try loading the source of the page instead of looking at it in a console (firbug, etc) to see what I mean.

Off the top of my head I think you're going to have to execute the contents of that youtube page on your server as a browser would to get access to the iframe. That could involve crawling the page with PHP, then passing the source into something that will process the HTML/JavaScript, then passing the result back to PHP to get the SRC of the iframes.

I'm not aware of any out-of-the-box PHP solutions for this. Maybe the following SO links will get you started:

I feel like this isn't really a complete answer, but I'm not allowed to leave comments yet so sorry in advance.

Upvotes: 0

Related Questions