帝国CMS发布信息时替换正文IMG图片标签里的ALT内容

wdzsjl 2周前 (10-10) 阅读数 6 #PHP

要在 EmpireCMS 中实现替换正文 IMG 标签中的 ALT 内容的功能,可以通过以下步骤进行:

  1. 在 e/class/userfun.php 文件中增加函数 user_imgalt

  2. 在后台系统设置中编辑 newstext 字段,添加相应的处理逻辑

步骤 1:在 e/class/userfun.php 文件中增加函数 user_imgalt

打开 e/class/userfun.php 文件,在合适的位置添加以下函数:

// 替换正文 IMG 标签中的 ALT 内容 function user_imgalt($mid, $f, $isadd, $isq, $value, $cs) {     // 获取文章标题     $title = $_POST['title'];     // 获取 HTML 内容     $htmls = $value;      // 匹配所有的 <img> 标签     $pattern = '/<img[^>]+>/';     preg_match_all($pattern, $htmls, $matches);      // 遍历匹配到的 <img> 标签     for ($i = 0; $i < count($matches[0]); $i++) {         // 匹配 <img> 标签中的 alt 属性         preg_match_all('/alt=[\'"](.+?)[\'"]/i', $matches[0][$i], $altimg);          // 判断是否有 alt 属性         if (empty($altimg[1])) {             // 如果没有 alt 属性,则添加文章标题作为 alt 属性             $htmls = str_replace($matches[0][$i], '<img' . substr($matches[0][$i], 4) . ' alt="' . htmlspecialchars($title) . '"', $htmls);         }     }      return $htmls; }

步骤 2:在后台系统设置中编辑 newstext 字段

  1. 登录 EmpireCMS 后台。
  2. 进入 系统设置 -> 数据表模型 -> 字段管理
  3. 找到 newstext 字段并编辑。
  4. 在 字段处理 中添加如下处理逻辑:
text
 
user_imgalt($mid,$f,$isadd,$isq,$value,$cs)
wx.jpg ywfw.jpg
热门