栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP > php开源框架 > dedecms

织梦在PHP7.0环境中为文章图片自动添加ALT属性为标题方法

dedecms 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

织梦在PHP7.0环境中为文章图片自动添加ALT属性为标题方法

织梦在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,}[sS]{0,}["'s] @isU"," ",$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,}[sS]{0,}["'s] @isU"," ",$newurl);
    $newurl=str_ireplace("","/>",$newurl);
    return $newurl;
     }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/7754.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号