tags:

views:

140

answers:

2

Hello,

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!

+3  A: 

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
Wow! The same snippet, 15 seconds apart. :)
Pekka
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
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
ok, thank you great!
elmaso
+2  A: 

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
+1 nice answer ;) but I beat you by 3 seconds LOL
Byron Whitlock
Yeah, but thats because I took the time to actually insert a link to the needed library :D
Pekka