muban大师 发表于 2020-3-21 13:09:33

dedecms织梦去掉文章内容中的图片宽度和高度限制的办法

在做响应式或者手机站的模板时候,我们经常会遇到图片因为长度和宽度固定,而无法自适应手机端的问题,原因就是是织梦的编辑器在上传图片时,会自动加上style属性,因此,在保存文章时,我们需要先清除掉这些属性。跟版网的小编最近也遇到这个问题,百度上搜了一下,有一种方法就是对body中的数据进行过滤,写法如下(在内容页找到{dede:field.body}修改为如下内容):
       
        {dede:field.body runphp=yes}
        global $cfg_basehost;
        $str = @me;
        $search = '/([i,,+>)/is';
        $search1 = '/([i,,+>)/is';
        $search2 = '#([i,)#i';
        $search3 = '#([i,)#i';
        $content = preg_replace($search,'$1$3',$str);
        $content = preg_replace($search1,'$1$3',$content);
        $content = preg_replace($search2,'$1$2',$content);
        $content = preg_replace($search3,'$1$2',$content);
        @me = $content;
        //@me = str_replace('/uploads/allimg/', $cfg_basehost.'/uploads/allimg/', $content);//手机版图片使用绝对路径
        {/dede:field.body}
       
        AB织梦模板网试了一下,我复制的内容中还有一些杂项其实还是没有过滤掉的。于是想到直接在保存时候进行处理,找到:/dede/article_add.php和/dede/article_edit.php 这两个文件(对应文章模型,其他模型请找后台对应的文件),搜索如下代码:
       
        $body = AnalyseHtmlBody($body,$description,$litpic,$keywords,'htmltext');
       
        在这段代码的后面加入:
       
        //去除img中的style属性
        $body = preg_replace("/style=\\\.+?['|\",/i",'',$body);
        //去除img中的width,height属性
        $exp=Array("/height=.{0]5}\s/i","/width=.{0]5}\s/i"); $exp_o=Array('','');
        $body = preg_replace($exp]$exp_o,$body);
       
        其实只改一个article_edit.php文件就行了,修改后发布或者修改文档时候,会自动去除掉body中的所有style。后者会把内容中所有的style都会被去掉,文档相当纯净。
       
        具体用哪种方法,大家可以根据需求而定。
页: [1]
查看完整版本: dedecms织梦去掉文章内容中的图片宽度和高度限制的办法