1

example: at this domain http://www.example.com/234234/go.html is only one iframe-code

how can i get the url in the iframe-code?

go.html:

<iframe style="width: 99%;height:80%;margin:0 auto;border:1px solid grey;" src="i want this url" scrolling="auto" id="iframe_content"></iframe>

i have this snippet, but its very bad coded..

function downloadlink ($d_id)
  {
    $res = @get_url ('' . 'http://www.example.com/' . $d_id . '/go.html');
    $re = explode ('<iframe', $res);
    $re = explode ('src="', $re[1]);
    $re = explode ('"', $re[1]);
    $url = $re[0];
    return $url;
  } 

thank you!

elmaso
  • 193
  • 1
  • 2
  • 14
  • I added a different method to my answer to your previous question: http://stackoverflow.com/questions/2563256/php-explode-and-get-url-not-showing-up-the-url/2563295#2563295 – Chad Birch Apr 01 '10 at 20:53

2 Answers2

3

Use a html parser such as simple_html_dom to parse html.

$html = file_get_html('http://www.example.com/');

// Find all iframes
foreach($html->find('iframe') as $element)
   echo $element->src . '<br>';
Byron Whitlock
  • 52,691
  • 28
  • 123
  • 168
  • Wow! The same snippet, 15 seconds apart. :) – Pekka Apr 01 '10 at 20:53
  • thank you, but the simple_html_dom.php has 36KB! :) that's big for this little function, or is there another smaller file to use? – elmaso Apr 01 '10 at 21:07
  • php can parse a 36kb file faster than it takes to read from the disk ;) Trust me the programmer speed is way more important than code speed in this case. – Byron Whitlock Apr 01 '10 at 21:09
2

I don't know what scope you have here - is it just that snippet, or are you browsing whole pages?

If you're browsing whole pages, you could use the PHP Simple HTML DOM Parser. A slightly modified example from their site:

// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');

// Find all iframes
foreach($html->find('iframe') as $element)
       echo $element->style . '<br>';

This sample code goes through all iframes on the page, and outputs their src property.

PHP has built-in functions for this as well (like SimpleXML), but I find the DOM Parser very nice and easy to handle (as you can see).

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • Yeah, but thats because I took the time to actually insert a link to the needed library :D – Pekka Apr 01 '10 at 21:07