删除(销毁)富文本框内的图片资源,编辑时删除(销毁)富文本框内删除或者替换的图片资源

/**
     * 删除富文本框内的图片资源(必须后台上传的,也不能是外连接),可以放在公共办法里直接调用
    */
    function del_fuwenben_img($content){
        $content = htmlspecialchars_decode($content);//首先转义
        preg_match_all("/src=[\'\"]\/public\/upload(.*?)[\'\"].*?/", $content, $match);//正则匹配
        foreach ($match[0] as $key => $value) {
            $limg = ltrim($value,'src="');
            $img = rtrim($limg,'"');
            if ($img && file_exists('.'.$img)) {
                unlink('.'.$img);//销毁图片
            }
        }
    }

 

/**
     * 编辑时删除(销毁)富文本框内删除或者替换的图片资源(必须后台上传的,也不能是外连接)
     * $content 是否需要删除的(原本的)
     * $content_edit 对比的(编辑过的)
    */
    function del_fuwenben_img_edit($content,$content_edit){

        $content_edit = htmlspecialchars_decode($content_edit);
        preg_match_all("/src=[\'\"]\/public\/upload(.*?)[\'\"].*?/", $content_edit, $match_edit);
        $img_edit = [];
        foreach ($match_edit[0] as $key => $value) {
            $limg = ltrim($value,'src="');
            $img_edit[] = rtrim($limg,'"');
        }

        $content = htmlspecialchars_decode($content);
        preg_match_all("/src=[\'\"]\/public\/upload(.*?)[\'\"].*?/", $content, $match);
        foreach ($match[0] as $key => $value) {
            $limg = ltrim($value,'src="');
            $img = rtrim($limg,'"');
            if(!in_array($img, $img_edit)){
                if ($img && file_exists('.'.$img)) {
                    unlink('.'.$img);
                }
            }
        }
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值