extracting all images urls ... (smart way)

Discussion in 'PHP' started by redhits, Oct 29, 2009.

  1. #1
    How i can extract all images links?

    I would do something like : excade('img src',$webpage)... but i am afraid an crazy coder could code something like img border=0 scr='image.jpg' , and it will mess everything up...

    So how i can extact all images urls in a smart way , using preg_match , to get something like img*src?
     
    redhits, Oct 29, 2009 IP
  2. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #2
    Assuming you have html page content in $html;
    
    $dom = new DOMDocument();
    @$dom->loadHTML($html);
    
    // grab all the on the page
    $xpath = new DOMXPath($dom);
    $imgs = $xpath->evaluate("/html/body//img");
    
    foreach ($imgs as $img) {
    	$image = $img->getAttribute('src');
    	echo "<br />image: $image";
    }
    
    PHP:
     
    AsHinE, Oct 29, 2009 IP