-1

How can i get multiple images using below script

<?php
$url = 'http://stackoverflow.com/questions/7994340/how-can-i-get-image-from-url-in-php-or-jquery-or-in-both';
$data = file_get_contents($url);

if(strpos($data,"<img"))
{
    $imgpart1 = explode('<img src=',$data);
    $imgpart2 = explode('"',$imgpart1[1]);
    echo "<img src=".$imgpart2[1]." />";
}
?>

Please help!

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • I'd suggest using [`DOMDocument`](http://php.net/manual/en/class.domdocument.php) to parse the HTML, verus using string searching. – gen_Eric Jun 27 '13 at 15:31

1 Answers1

0

You want to use a HTML/DOM parser for this, it's not a job for regexes or string searching.

I like PHP's DOMDocument, it's not too hard to use.

$url = 'http://stackoverflow.com/questions/7994340/how-can-i-get-image-from-url-in-php-or-jquery-or-in-both';
$data = file_get_contents($url);

$dom = new DOMDocument;
// I usually say to NEVER use the "@" operator,
// but everyone's HTML isn't perfect, and this may throw warnings
@$dom->loadHTML($data);

$img = $dom->getElementsByTagName('img');
foreach($img as $x){
    echo $x->getAttribute('src');
}
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • Perfect its working fine.. thank you very much for help. other developer also who took the time for me. thanks. – user2528747 Jun 27 '13 at 16:08