Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi im trying to fetch images from url using below code but it display only one img ,
can any one sujjest how to do this

XML
<?php
//$url="http://www.askmen.com/fine_living/wine_dine_archive_500/520_mezcal-and-tequila.html";
$url="http://en.wikipedia.org/wiki/Wonders_of_the_World";
$url2="";
$html = file_get_contents($url);

preg_match("/<img .+?\/>/",$html, $result);
//print_r($result);
 for($x=0;$x<count($result);$x++)
 {
   echo $result[0];
 }
?>


i have modified my code , the problem is that it is taking names of all images but not going to display it .
can any one have the soln ?

here is my updated code
XML
$url="http://www.annkristinhaugen.no";
$html = file_get_contents($url);

preg_match("/<img .+?\/>/",$html, $result);

$doc=new DOMDocument();
@$doc->loadHTML($html);

$xml=simplexml_import_dom($doc); // just to make xpath more simple
$images=$xml->xpath('//img');
$arr=array();
foreach ($images as $img) {
    $arr[]=$img['src'];
}


 for($x=0;$x<count($arr);$x++)
    {
?>      <?php //echo $arr[$x][0];?>
    <img src="<?php echo $arr[$x][0];?>" width="150" height="150"/>
<?php


}
?>
Posted
Updated 14-Mar-12 20:24pm
v6

1 solution

Try these links and see if they solve your problem. Some say preg_match doesn't do a great job with angle brackets, not sure about it though:

Get Images from URL[^]

http://stackoverflow.com/questions/138313/how-to-extract-img-src-title-and-alt-from-html-using-php[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900