帝國CMS截取內(nèi)容簡介函數(shù),過濾其中特殊字符及空格
帝國cms內(nèi)容頁模板的描述標(biāo)簽,是直接輸出內(nèi)容標(biāo)題,這個在seo優(yōu)化當(dāng)中還是有一定影響的,新聞在發(fā)布時會自動生成smalltext簡介字段,但我們?nèi)绻苯釉陧撁嫔陷敵龊喗樽侄危?jīng)常會帶有特殊字符或者帶有換行,這個也是非常不好的。接下來跟cms大學(xué)小編一起學(xué)習(xí)如何在帝國cms的內(nèi)容頁輸出不換行且沒有特殊字符的內(nèi)容簡介。
函數(shù)代碼如下,:
function Cmsdx_format_html($str){
$str=trim($str);
$str=str_replace('&','',$str);
$str=str_replace('ldquo;','“',$str);
$str=str_replace('rdquo;','”',$str);
$str=str_replace('middot;','·',$str);
$str=str_replace('lsquo;','‘',$str);
$str=str_replace('rsquo;','’',$str);
$str=str_replace('hellip;','…',$str);
$str=str_replace('mdash;','—',$str);
$str=str_replace('ensp;','',$str);
$str=str_replace('emsp;','',$str);
$str=str_replace('nbsp;','',$str);
$str=str_replace(' ','',$str);
$str=str_replace('/t','',$str);
$str=str_replace('/r/n','',$str);
$str=str_replace('/r','',$str);
$str=str_replace('/n','',$str);
$str=str_replace(' ','',$str);
$str = preg_replace('//s(?=/s)/','', $str);// 接著去掉兩個空格以上的
$str = preg_replace('/[/n/r/t]/',' ', $str);// 最后將非空格替換為一個空格
return trim($str);
}
我們將上述函數(shù)放到 /e/class/userfun.php 中,這里是存儲用戶的自定義函數(shù)。
接下來在內(nèi)容頁描述的meta標(biāo)簽中調(diào)用如下標(biāo)簽:
<?=Cmsdx_format_html($navinfor['smalltext'])?>
注意外層一定要包裹我們寫的自定義函數(shù),這樣就可以實(shí)現(xiàn)無特殊格式的輸出smalltext簡介字段了。