header('Content-Type: text/html; charset=utf-8');
/**
* $str,要进行处理的内容
* $ext,要匹配的扩展名
*/
function img_match($str,$ext){
$list = array();
//先取出所有img标签文本
$c1 = preg_match_all('//', $str, $m1);
//对所有的img标签进行取属性
for($i=0; $i
//匹配出所有的属性
$c2 = preg_match_all('/(\w+)\s*=\s*(?:(?:(["\'])(.*?)(?=\2))|([^\/\s]*))/', $m1[0][$i], $m2);
//将匹配完的结果进行结构重组
for($j=0; $j
$list[$i][$m2[1][$j]] = !empty($m2[4][$j]) ? $m2[4][$j] : $m2[3][$j];
}
}
return $list;
}
//罗列图片后缀从而实现多扩展名匹配
$ext = 'jpg|jpeg|gif|bmp|png';
//模仿一个网站
$str = <<<___>
阅谁问君诵,水落清香浮onestopweb.iteye.com
___;
//测试效果
$img_list = img_match($str,$ext);
var_dump($img_list);
效果图: