PHP通过正则表达式获取网页中的所有图片

at 7年前  ca 记录笔记  pv 1224  by authorhu  

  1. <?php 
  2.     $url="http://sports.qq.com/photo/?pgv_ref=aio"; 
  3.     //file_get_contents() 函数把整个文件读入一个字符串中 
  4.     $string=file_get_contents($url); 
  5.     //preg_match_all函数进行全局正则表达式匹配。 
  6.     preg_match_all("/<img([^>]*)\s*src=('|\")([^'\"]+)('|\")/", 
  7.                     $string,$matches);//带引号 
  8.     //preg_match_all("/<img([^>]*)\ssrc=([^\s>]+)/",$string,$matches);//不带引号 
  9.     $new_arr=array_unique($matches[0]);//去除数组中重复的值 
  10.     foreach($new_arr as $key){ 
  11.         echo $key."</br>"; 
  12.     
  13. ?>
帝国cms替换newstext字段的IMG 字段增加处理函数user_pic <?php //---------------------------用户自定义标签函数文件 function user_pic($mid,$f,$isadd,$isq,$value,$cs){ $string=$value; preg_match_all('/<img src=\"([^"]*)\"\/>/',$string,$matches);//不带引号 $new_arr=array_unique($matches[1]);//去除数组中重复的值 foreach($new_arr as $key){ $returnstr='<a id="example1" href="'.$key.'"><img src="'.$key.'" /></a>'; } return $returnstr; } ?>

版权声明

本文仅代表作者观点,未经许可,不得转载。

分享:

支付宝

微信