0

I'm attempting to build a function that will get images from a URL and sort through the values in the array and save the ones that contain only the src of the image. If you use this function it will pull images with the tags as well as the image src.

What I am asking is how can I get the src of image from a given URL put into a simple array?

function getImagesFromURL($url){
    $crl = curl_init();
    $timeout = 5;
    curl_setopt ($crl, CURLOPT_URL,$url);
    curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
    $ret = curl_exec($crl);

    if(preg_match_all('!<img.*?src=\"(.*?)\".*?>!is', $ret, $images)){
        print_r($images);
    }
    else {
        return "No images found!";
    }
}
Michael
  • 495
  • 1
  • 11
  • 29
  • Firstly: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags . Secondly, if you still think using regex is a good idea, you should use `preg_match` in a loop, and take the value of the capture group for the content in the src field. – yiding May 09 '13 at 02:11
  • Thank you for that information but what would you recommend instead ( a link would suffice)? Also, I am using a regex to capture the and <meta description=""/> of a page, is ok or would you also not recommend it for that use as well? – Michael May 09 '13 at 02:26
  • Use the [DOM library that comes with php](http://docs.php.net/manual/en/domdocument.loadhtml.php). – yiding May 09 '13 at 22:16

0 Answers0