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}>
我的QQ群:
PHPer&Webgame&移动开发,群号:95303036