帝国CMS内容页如何替换img标签的alt和title为标题?
网上的方法基本会出现两次alt标签,对SEO不利。
特科网正文图片alt标签和title标签效果图

下面分享特科网自用的方法:
1)增加自定义函数
在 e/class/userfun.php 里面增加下列函数
//替换img的url,alt,title-修改Editer内容时
function user_AlterEditer($mid,$f,$isadd,$isq,$value,$cs){
$title=str_replace('"','',$_POST['title']);
$zhengze1="/<img([^>]*?)src=[\"']\/d\/file\/([^\"']+)[\"'][^>]*>/i";
$value=preg_replace($zhengze1,"<img src=\"https://img.teke.cn/\\2\">",$value);
$imgUrl = "<img\s[^>]*src=(\"??)([^\" >]*?)\\1[^>]*>";
if(preg_match_all("/$imgUrl/siU",$value,$matches,PREG_SET_ORDER)){
for ($i=0; $i < count($matches); $i++){
$before = $matches[$i][0];
$srcValue = $matches[$i][2];
$newImg = '<img src="'.$srcValue.'" alt="'.$title.' 第'.($i+1).'张" />';
$value = str_replace($before,$newImg,$value);
}
}
return $value;
}
//替换img的url,alt,title-新增Editer内容时
function user_AddEditer($mid,$f,$isadd,$isq,$value,$cs){
$title=str_replace('"','',$_POST['title']);
$zhengze="/<img([^>]*?)src=[\"']\/d\/file\/([^\"']+)[\"'][^>]*>/i";
$value=preg_replace($zhengze,"<img src=\"https://img.teke.cn/\\2\">",$value);
$imgUrl = "<img\s[^>]*src=(\"??)([^\" >]*?)\\1[^>]*>";
if(preg_match_all("/$imgUrl/siU",$value,$matches,PREG_SET_ORDER)){
for ($i=0; $i < count($matches); $i++){
$before = $matches[$i][0];
$srcValue = $matches[$i][2];
$newImg = '<img src="'.$srcValue.'" alt="'.$title.' 第'.($i+1).'张" />';
$value = str_replace($before,$newImg,$value);
}
}
return $value;
}注意:
将代码中的img.teke.cn改成自己的网址,如www.abc.com
将代码中的https://img.teke.cn改成http://www.abc.com
2)设置函数执行时间
位置:后台>系统设置>数据表与系统模型>管理数据表,点击管理字段。

然后点击字段newstext后面的“修改”。
在“后台增加信息处理函数”和“后台修改信息处理函数”后添加user_AlterEditer,如图:

此教程分享完毕,后面发布的内容图片alt和title标签将和特科网一样。