接口去除标签并且提取图片视频

public function contents($content)
{
$content = preg_replace('/<img\s.*?src="(.*?)"\/>/','{[src="$1"{[',$content);//匹配图片
$content = preg_replace('/<iframe\s(.*?src=(.*?)\s.*?)<\/iframe>/','{[$1{[',$content);
$content = preg_replace('/<a\shref=("\/[a-z]{3,}\/[a-z]{3,}\/[0-9]{2,15}\.html"\s.*?>.*?)<\/a>/','{[$1{[',$content);//匹配a链接
$content = preg_replace('/<\/li>/',PHP_EOL,$content);
$content = preg_replace('/&nbsp;/','',$content);
$content = preg_replace('/<\/br>/',PHP_EOL,$content);
$content = preg_replace('/<\/p>/',PHP_EOL,$content);
$content = strip_tags($content);
if($content)
{
$content = explode('{[',$content);
if($content)
{
foreach($content as $k => $v)
{
if(trim($v,PHP_EOL))
{
$ter = preg_match('/"\/.*?\/[\w\-]{8,11}\/[A-Za-z0-9]{10,20}.[a-z]{3,4}"/',$v,$m);//匹配图片
$href = preg_match('/"\/[a-z]{3,}\/[a-z]{3,}\/([0-9]{2,15})\.html"/',$v,$h);//匹配链接
$movie = preg_match('/http:.*?[",\s]/',$v,$e);//匹配视频
$e[0]=trim($e[0]);//去两边字符
$e[0] = str_replace('"','',$e[0]);//把双引号去掉
if($ter)
{
$m = str_replace('"','',$m);
$size = getimagesize('../thinkphp'.$m[0]);
//服务器上图片使用路径
$m = preg_replace('/\/ueditor/','http://www.acg170.com/ueditor',$m);
$m = preg_replace('/\/Public/','http://www.acg170.com/Public',$m);
$arr[$k][0] = 'img';
$arr[$k][1] = [[$m[0],$size[0],$size[1]],''];
}elseif($movie)
{
$arr[$k][0] = 'movie';
$hei = preg_match('/height[=,:]([0-9]{3})/',$v,$height);
if ($hei) {
$arr[$k][1] = [$e[0],$height[1]];
}else{
$arr[$k][1] = [$e[0],''];
}
}elseif($href){//a链接
$arr[$k][0] = 'link';
preg_match('/[0-9]{2,}/',$v,$id);//获取数据id
preg_match('/">([\s\S]*)/',$v,$text);//获取文字内容
$arr[$k][1] = [$id[0],$text[1],'0'];
}else{
$arr[$k][0] = 'text';

$arr[$k][1] = [$v];
}
}
}
}
}
return $arr;
}

猜你喜欢

转载自www.cnblogs.com/yang1022/p/10076057.html