php获取文章中图片img标签方法,适用于抓取图片,这里提供一个基本的方法,具体可以根据自己应用的需要进行拓展。
// 获取文章中图片img标签
function preg_image_html($content, $length = 9, $w = 100, $h = 100) {
preg_match_all ( '#<img.*?src="([^"]*)"[^>]*>#i', $content, $match );
$count_img = 0;
foreach ( $match [1] as $ik => $imgurl ) {
if ($count_img >= $length)
break;
if (! empty ( $imgurl )) {
// 过滤系统自带编辑器表情图片
if (preg_match ( '#^http://img.baidu.com/hi/#', $imgurl )) {
unset ( $match [1] [$ik] );
continue;
}
if (preg_match ( '#Uploads/Smiley/#', $imgurl )) {
unset ( $match [1] [$ik] );
continue;
}
$return [] = array (
0 => $imgurl,
1 => mk_rule_image ( $imgurl, $w, $h )
);
$count_img ++;
}
}
return $return;
}