• 全国400电话网上服务平台
    强大号码库资源任选,方便,快捷,快速开通。
    咨询热线:400-1100-266  

    织梦在PHP7.0环境中为文章图片自动添加ALT属性为标题方法
    POST TIME:2021-05-23 02:40

    织梦在PHP7.0环境中为文章图片自动添加ALT属性为标题方法,首先修改/include/extend.func.php,里面的把这段代码加进去:

     
    //文章body优化替换
            function replaceurl($newurl)
            {
            global $dsql,$id;
            //获取图片附加表imgurls字段内容进行处
            $row = $dsql->GetOne("SELECT title FROM dede_archives where id=$id");
            //替换图片Alt为文档标题
            $newurl=str_ireplace(array('alt=""','alt=\&;\&;'),'',$newurl);
            $newurl=preg_replace("@ [\s]{0,}alt[\s]{0,}=[\&;'\s]{0,}[\s\S]{0,}[\&;'\s] @isU"," ",$newurl);
            $newurl=str_ireplace("<img " ,"<img  alt=\&;".$row['title']."\&;",$newurl);
            //去掉结尾空格
            $newurl=str_ireplace(" /","/",$newurl);
            $newurl=str_ireplace(" />","/>",$newurl);
            return $newurl;
            }
     

    然后在我们需要调用织梦文章内容的地方加上调用代码:

    {dede:field.body function='replaceurl(@me)'/}

     

    由于之前在《织梦如何去除img图片中的style width height属性》一文中,实现去高宽属性,既然我们今天用自定义函数实现了替换alt属性,那么也可以把去高宽属性的这个功能加进去,完整代码如下:

    //文章body优化替换
        function replaceurl($newurl)
         {
        global $dsql,$id;
        //获取图片附加表imgurls字段内容进行处
        $row = $dsql->GetOne("SELECT title FROM dede_archives where id=$id");
        //去掉img的width和height
        $newurl=preg_replace('/style=\&;width\:(.*)\&;/','',$newurl);
        //替换图片Alt为文档标题
        $newurl=str_ireplace(array('alt=""','alt=\&;\&;'),'',$newurl);
        $newurl=preg_replace("@ [\s]{0,}alt[\s]{0,}=[\&;'\s]{0,}[\s\S]{0,}[\&;'\s] @isU"," ",$newurl);
        $newurl=str_ireplace("<img " ,"<img  alt=\&;".$row['title']."\&;",$newurl);
        //去掉结尾空格
        $newurl=str_ireplace(" /","/",$newurl);
        $newurl=str_ireplace(" />","/>",$newurl);
        return $newurl;
         }

    
    关于我们 | 付款方式 | 建站知识 | 增值服务 | 网站模板
    Copyright © 2003-2016
    时间:9:00-21:00 (节假日不休)
    版权所有:巨人网络(扬州)科技有限公司
    总部地址:江苏省信息产业基地11号楼四层
    《增值电信业务经营许可证》 苏B2-20120278
    X

    截屏,微信识别二维码

    微信号:veteran88

    (点击微信号复制,添加好友)

     打开微信

    微信号已复制,请打开微信添加咨询详情!