本文介绍如何在发布文章的时候自动给内容中的图片加上ALT和TITLE属性,并将文章标题作为两个属性的值。
按步骤进行操作:
1、复制以下文本里的字段处理函数放入 e/class/userfun.php 文件中,可放最后面 ?>之前
function user_AddImgAlt($mid,$f,$isadd,$isq,$value,$cs){
$title=$_POST['title'];
$htmls=$value;
$pattern = "/<img[^>]+>/";
preg_match_all($pattern, $htmls, $matches);
for ($i=0; $i<=count($matches[0]); $i++) {
preg_match_all("/alt=\".+?\"/",$matches[0][$i],$altimg);
preg_match_all("/title=\".+?\"/",$matches[0][$i],$titleimg);
$t_alt=count($altimg[0]);
if($t_alt==0){
$htmls=str_replace("<img","<img alt=\"{$title}\"",$htmls);
$htmls=str_replace("<img","<img title=\"{$title}\"",$htmls);
}
}
return $htmls;
}
2、修改数据表中的新闻正文字段(newstext)
需要将对应数据表模型中的字段进行修改。新闻正文字段一般是newstext(也可以是其它字段),在字段处理函数文本框填上user_AddImgAlt 如下图所示就行。

修改字段

修改信息处理字段
补充说明:这个函数增加alt内容是强制性的,如果手动添加了alt也会被替换成文章标题。
以上就是帝国CMS新闻正文图片自动加alt与title为文章标题方法。