dedecms文章内容自动给内容中的图片添加alt属性
在dedecms中想给内容自动添加alt属性,并且实现alt为文章标题,其调用代码如下:
{dede:field.body runphp=yes}
global $dsql,$id,$aid;
$myid = isset($id) ? $id : $aid;
$row = $dsql->GetOne("SELECT title FROM `#@__archives` WHERE id=$myid");
$title = $row['title'];
$str = @me;
$search = '/(<img.*?)alt=(["\'])?.*?(?(2)\2|\s)([^>]+>)/is';
$search1 = '/(<img.*?)title=(["\'])?.*?(?(2)\2|\s)([^>]+>)/is';
$content = preg_replace($search,'$1$3',$str);
$content = preg_replace($search1,'$1$3',$content);
@me = str_replace('<img', "<img alt='{$title}' title='{$title}'", $content);
{/dede:field.body}
通过上面的代码可以自动实现给文章主体内容中的img添加alt属性,属性值为title。例如:圆形木模板

浙公网安备 33010602011771号