从preg_match获取图片,判断是否存在然后将图片传到foreachpreg_match获取的图片网址类似这样:
http://www.a.com/1.jpg根据下面网址判断图片是否存在,后面多了个后缀:
http://www.a.com/1.jpg-ext判断方式:如果页面内容如下,即为不存在,否则为存在:
{"error":"Document not found"}
(注:图片不存在,打开图片网址时,这段内容就是网页的全部源码。)如果图片存在,把图片网址值传到下面这个foreach循环里:
  <?php
  preg_match_all('/(?<=(src\=\"))(.*?)(?=\.jpg)/', $dpic['detail'], $matches);
  unset($matches[1]);
  unset($matches[2]);
  foreach($matches as $key=>$value) 
  {
  for($i=0;$i<count($value);$i++)
  {
  echo '<img src=';
  echo $value[$i];
  echo '.jpg-ext"/>';
  }
}
  ?>

解决方案 »

  1.   

    提供检查远程图片是否存在方法<?php
    $url = 'http://php.net/images/logo.php';$ret = checkRemoteUrl($url);
    var_dump($ret);function checkRemoteUrl($url){
        $result = get_headers($url, true);
        if(isset($result[0])){
            $tmp = explode(' ', $result[0]);
            if(isset($tmp[1]) && $tmp[1]=='200'){
                return true;
            }
        }
        return false;
    }