1. 程式人生 > >接口去除標簽並且提取圖片視頻

接口去除標簽並且提取圖片視頻

lin 匹配 [1] ima height .com rep ace src

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;
}

接口去除標簽並且提取圖片視頻