thinkphp删除文章里的所有图片

xiaoxiao2021-02-28  87

/*删除文章内容图片(也就是删除编辑器上传的图片)*/ function delete_img($content){ //匹配并删除图片 $img_path = "/<img.*src=\"([^\"]+)\"/U"; $matches = array(); preg_match_all($img_path, $content, $matches); foreach($matches[1] as $img_url){ //strpos(a,b) 匹配a字符串中是否包含b字符串 包含返回true if(strpos($img_url, 'emoticons')===false){ $host = 'http://' . $_SERVER['HTTP_HOST'] . '/'; $filepath = str_replace($host,'',$img_url); if($filepath == $img_url) $filepath = substr($img_url, 1); @unlink($filepath); $filedir = dirname($filepath); $files = scandir($filedir); if(count($files)<=2)@rmdir($filedir);//如果是./和../,直接删除文件夹 } } unset($matches); }
转载请注明原文地址: https://www.6miu.com/read-56863.html

最新回复(0)