- function smarty_modifier_cn_truncate($string, $length = 80, $etc = '...', $count_words = true,$w_tags=false)
- { if($w_tags==true){
- $string=strip_tags($string);
- }
- $wordscut = '';
- mb_internal_encoding("UTF-8");
- if ($length == 0)return '';
- if ( strlen( $string ) <= $length ) return $string;
- preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $info);
- if( $count_words ){
- $j = 0;
- for($i=0; $i<count($info[0]); $i++) {
- $wordscut .= $info[0][$i];
- if( ord( $info[0][$i] ) >=128 ){
- $j = $j+2;
- }else{
- $j = $j + 1;
- }
- if ($j >= $length ) {
- return $wordscut.$etc;
- }
- }
- return join('', $info[0]);
- }
- return join("",array_slice( $info[0],0,$length ) ).$etc;
- }
cn_truncate函數(shù)目的:
1、在模板中直接截取中文字符
2、可保留原來的字符串style,如 font 等
用法:
cn_truncate 用法大致同官方的 truncate 函數(shù),如下:
{$item.title|cn_truncate:18:"...":true}
第1個參數(shù) 18 表示截取 18 個漢字
第2個參數(shù) ... 表示,如果多余18個漢字,則顯示 ...
第3個參數(shù) true 表示保留文字的初始顏色。 false 表示去掉顏色。
應(yīng)用實例:可以在首頁調(diào)用公司介紹等等
這個函數(shù)唯一不好的就是他不能去掉html標簽,有時候是需要截取新聞前面一句話的,不包括里面的html,這個該怎么操作呢,只需要把函數(shù)改變下就行了
- function smarty_modifier_cn_truncate($string, $length = 80, $etc = '...', $count_words = true,$w_tags=false)
- { if($w_tags==true){
- $string=strip_tags($string);
- }
把函數(shù)上面改成這樣就可以了,調(diào)用的時候取$w_tags=true即可。例如{$item.title|cn_truncate:18:"...":true:true},好了,還有什么不明白的可以到程序員人生網(wǎng)站留言,謝謝大家
上一篇 如何注冊百度聯(lián)盟廣告
下一篇 商品列表彈出遮罩層