3

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! :)

Anders
  • 513
  • 2
  • 10
  • 32
  • 3
    Aaaaaaand what did you try so far? This is stackoverflow, not yahoo answers. Edit: all right, he wrote something now :) – briosheje Dec 20 '13 at 17:07
  • Using something like `simple_html_dom` or `DOM` you can iterate through `iframe` elements and get the source. This is very basic if you look at the documentation. – h2ooooooo Dec 20 '13 at 17:08
  • 2
    Use a DOM parser (PHP's [`DOMDocument`](http://php.net/dom), for example) to achieve this. You can use `foreach($dom->getElementsByTagName('iframe') as $iframe) { ... }`. – Amal Murali Dec 20 '13 at 17:09
  • @Anders: Where exactly do you have issues? What is not working? – Amal Murali Dec 20 '13 at 17:10
  • indeed, as @AmalMurali said, you should loop through all 'iframe' objects of your DOMDocument. Anyway, what's going wrong with the output of your above code? It absolutely seems legit to me :) – briosheje Dec 20 '13 at 17:11
  • It doesn't echo any iframes. That's my problem – Anders Dec 20 '13 at 17:12
  • What is contained in your $html value? are you sure you're passing the right string to it? – briosheje Dec 20 '13 at 17:13
  • $i must have a starting value like $i=0. this should solve your problem – Dwza Dec 20 '13 at 17:14
  • Right, indeed, $i should be 0, for instance change `for ($i; $i < $a->length; $i++)` with `for ($i = 0; $i < $a->length; $i++)` :) – briosheje Dec 20 '13 at 17:15
  • @Anders: Can you update the question with a sample of the HTML you're trying to parse (or the URL of the website) and the **expected output**? – Amal Murali Dec 20 '13 at 17:15
  • I have updated. Thank you! – Anders Dec 20 '13 at 17:21

1 Answers1

0

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.

Community
  • 1
  • 1
frederickf
  • 1,481
  • 13
  • 8