php删除编辑器内容中的图片文件方法

浏览:884 发布日期:2017/04/11 分类:技术分享 关键字: 删除编辑器图片
/*删除文章内容图片(也就是删除编辑器上传的图片)*/
function remove_content_img($content){
    //匹配并删除图片
    $imgreg = "/<img.*src=\"([^\"]+)\"/U";

    $matches = array();
    preg_match_all($imgreg, $content, $matches);

    foreach($matches[1] as $img_url){
        if(strpos($img_url, 'emoticons')===false){
            $web_root = 'http://' . $_SERVER['HTTP_HOST'] . '/';
            $filepath = str_replace($web_root,'',$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);
}
最佳答案
评论( 相关
后面还有条评论,点击查看>>