在smarty/plugins/下,新建文件:modifier.truncate_cn.php
内容如下:
<?php
/*
*作者:http://www.phpernote.com/
*时间:2013年1月31日06:31:52
*作用:截取中文字符串
*/
function smarty_modifier_truncate_cn($string,$length=0,$ellipsis='…',$start=0){
$string=strip_tags($string);
$string=preg_replace('/\n/is','',$string);
//$string=preg_replace('/ | /is','',$string);//清除字符串中的空格
$string=preg_replace('/ /is','',$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,$string);
if(is_array($string)&&!empty($string[0])){
$string=implode('',$string[0]);
if(strlen($string)<$start+1){
return '';
}
preg_match_all("/./su",$string,$ar);
$string2='';
$tstr='';
//www.phpernote.com
for($i=0;isset($ar[0][$i]);$i++){
if(strlen($tstr)<$start){
$tstr.=$ar[0][$i];
}else{
if(strlen($string2)<$length+strlen($ar[0][$i])){
$string2.=$ar[0][$i];
}else{
break;
}
}
}
return $string==$string2?$string2:$string2.$ellipsis;
}else{
$string='';
}
return $string;
}
?>
<div id="slider">
{foreach $NewsRotaryView as $value}
<div>
<a lazyload="{$value.bpic}"></a>
<p>{$value.title}</p>
</div>
{/foreach}
</div>
调用插件功能就报错了,写法如下:
<div id="slider">
{foreach $NewsRotaryView as $value}
<div>
<a lazyload="{$value.bpic}"></a>
<p>{$value.title|truncate_cn:'5'}</p>
</div>
{/foreach}
</div>
求指点。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
<divclass="ref">