在 e/class/userfun.php 里面增加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | //替換正文IMG里的ALT內容 function user_imgalt( $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 ); $t_alt = count ( $altimg [0]); if ( $t_alt ==0){ $htmls = str_replace ( "<img" , "<img alt=/"{$title}/"" , $htmls ); } } return $htmls ; } |
然后在后臺系統設置-數據表模型-字段管理里面 編輯 newstext字段
增加如下
即可,圖片上的alt就自動被替換成了文章標題 適合seo!
下面是改進代碼 防止多個alt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | //替換正文IMG里的ALT內容 function user_imgalt($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); $t_alt=count($altimg[0]); if($t_alt==0){ //$htmls=str_replace("< img ","<img alt=/"{$title}/"",$htmls); $ htmls = str_replace ('/<img.+?src=/"(.+?)/".+?>/','< img src = "/1" alt = "{$title}" >',$htmls); } } return $htmls; } |
新聞熱點
疑難解答