/*** 给img标签加alt属性** @param string $content [html数据]* @param string $alt[alt属性值]*/function addAltInfo($content, $alt = '') {$content = str_replace('\\', '', (string) $content);preg_match_all('/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $content, $matches); //原img标签$origin = array();//替换成加alt属性的img$replace = array();foreach ($matches[2] as $key => $value) {//如果有alt属性则略过if (false !== strpos($matches[0][$key], 'alt')) {continue ;}$origin[] = $matches[0][$key];$replace[] = "<img src='$value' alt='$alt' />";}$content = str_replace($origin, $replace, $content);return detectlashes($content);}
如果觉得《php给img标签加alt属性》对你有帮助,请点赞、收藏,并留下你的观点哦!