smarty_modifier_truncate,无或者有md_substr的情况下都能正确截取字符串的php函数,可用于smarty。...

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}&gt;
|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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值