smarty_modifier_truncate,無或者有md_substr的情況下都能正确截取字元串的php函數,可用于smarty。
function smarty_modifier_truncate($string, $length = 80, $etc = '...', $code='utf8', $mb=true) { if ($length == 0) return $string; if(function_exists("mb_substr")&&$mb){ if(mb_strlen($string,$code)>$length) { return mb_substr($string, 0, $length,$code).$etc; } }else{ switch($code) { case "gb2312": case "gbk": $wordlen=2; break; case "utf8": $wordlen=3; break; default: $wordlen=1; break; } $strlen=strlen($string); $i=0; $len=0; $ct=0; while($i<$strlen) { if(ord($string[$i])<128) { $ct++; $len=0; }else{ $len++; if($len==$wordlen) { $ct++; $len=0; } } $i++; if($ct==$length) break; } return substr($string,0,$i); } return $string; }
<{section name=key loop=資料變量 max=顯示行數 fetch=fetch debug=1}> 設定debug=1,會将第一條資料的結構顯示出來。
<{$fetch.資料健名}>
<{$fetch.資料健名|strip_tags:0:1|truncate:截取長度:表示省略的符号:utf8}>
|strip_tags:0:表示去掉html标簽,:1:0:utf8對utf8編碼的字元串進行截取時需要,我們的是utf8編碼。
;
<{/section}>;
<{section name=key loop=$rs1 max=3 fetch=fetch debug=1}><{$fetch.ARTICLE_TITLE}>
<{$fetch.ARTICLE_TITLE|strip_tags:0:1|truncate:17:"...":1:0:utf8}>
<{$fetch.ARTICLE_CONTENT|strip_tags:0:1|truncate:60:"...":utf8}>;
<{/section}>
轉載于:https://www.cnblogs.com/Iamlein/archive/2008/06/18/2375939.html