php 正则提取内容的中的图片

直接上代码

1、全匹配

preg_match_all('/<img[\s\S]*?src\s*=\s*[\"|\'](.*?)[\"|\'][\s\S]*?>/i', $content, $result, PREG_SET_ORDER);

2、配置指定类型的图片

function getImgs($content,$num = 0){
	$pattern="/<img.*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.jpeg]))[\'|\"].*?[\/]?>/";
	preg_match_all($pattern, $content, $match);
	foreach($match[1] as $key=>$v) {
		$img[] = $v;
	}
	if($num == 1) {
		return count($img);
	} else {
		return $img;
	}
}

猜你喜欢

转载自blog.csdn.net/liuxl57805678/article/details/102975466