估计不少玩smarty模板的小朋友都遇到过裁切乱码问题。特别是UTF8编码的。
以下代码保存为modifier.truncate2.PHP 存到smarty libs下的plugin目录下
然后裁剪的时候用$v->content|truncate2:100
就搞定了。
如果不好用可能是缓存导致,请速度删除templates_c下的缓存文件(小编搞的时候遇到缓存问题。)
代码如下:
PHP
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty truncate modifier plugin
*
* Type: modifier@H_301_22@
* Name: truncate@H_301_22@
* Purpose: Truncate a string to a certain length if necessary,
* optionally splitting in the middle of a word,and
* appending the $etc string or inserting $etc into the middle.
* @link http://smarty.PHP.net/manual/en/language.modifier.truncate.PHP
* truncate (Smarty online manual)
* @author Monte Ohrt
* @param string
* @param integer
* @param string
* @param boolean
* @param boolean
* @return string
*/
function smarty_modifier_truncate2( $string,$length = 80,$etc='...',$count_words = true ) {
return $returnstr =substr_utf8($string,$length).$etc; }function substr_utf8($str,$start=0,$length=-1,$return_ary=false) {
$len = strlen($str);if ($length == -1) $length = $len;
$r = array();
$n = 0;
$m = 0; for($i = 0; $i < $len; $i++) {
$x = substr($str,$i,1);
$a = base_convert(ord($x),10,2);
$a = substr('00000000'.$a,-8);
if ($n < $start) {
if (substr($a,1) == 0) {
}elseif (substr($a,3) == 110) {
$i += 1;
}elseif (substr($a,4) == 1110) {
$i += 2;
}
$n++;
}else {
if (substr($a,1) == 0) {
$r[] = substr($str,1);
}elseif (substr($a,3) == 110) {
$r[] = substr($str,2);
$i += 1;
}elseif (substr($a,4) == 1110) {
$r[] = substr($str,3);
$i += 2;
}else {
$r[] = '';
}
if (++$m >= $length) {
break;
}
}
} return $return_ary ? $r : implode("",$r);
}
/* vim: set expandtab: */
?>
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty truncate modifier plugin
*
* Type: modifier@H_301_22@
* Name: truncate@H_301_22@
* Purpose: Truncate a string to a certain length if necessary,
* optionally splitting in the middle of a word,and
* appending the $etc string or inserting $etc into the middle.
* @link http://smarty.PHP.net/manual/en/language.modifier.truncate.PHP
* truncate (Smarty online manual)
* @author Monte Ohrt
* @param string
* @param integer
* @param string
* @param boolean
* @param boolean
* @return string
*/
function smarty_modifier_truncate2( $string,$length = 80,$etc='...',$count_words = true ) {
return $returnstr =substr_utf8($string,$length).$etc; }function substr_utf8($str,$start=0,$length=-1,$return_ary=false) {
$len = strlen($str);if ($length == -1) $length = $len;
$r = array();
$n = 0;
$m = 0; for($i = 0; $i < $len; $i++) {
$x = substr($str,$i,1);
$a = base_convert(ord($x),10,2);
$a = substr('00000000'.$a,-8);
if ($n < $start) {
if (substr($a,1) == 0) {
}elseif (substr($a,3) == 110) {
$i += 1;
}elseif (substr($a,4) == 1110) {
$i += 2;
}
$n++;
}else {
if (substr($a,1) == 0) {
$r[] = substr($str,1);
}elseif (substr($a,3) == 110) {
$r[] = substr($str,2);
$i += 1;
}elseif (substr($a,4) == 1110) {
$r[] = substr($str,3);
$i += 2;
}else {
$r[] = '';
}
if (++$m >= $length) {
break;
}
}
} return $return_ary ? $r : implode("",$r);
}
/* vim: set expandtab: */
?>
samrty的插件体系还是比较智能而且容易修改的。