解决Smarty中trancate使用UTF8时中文乱码问题
2008-11-18 22:46 Jaypei 阅读(606) 评论(0) 编辑 收藏 举报新建文件modifier.utruncate.php :
function smarty_modifier_utruncate($string, $length = 80, $etc = '...',
$break_words = false, $middle = false)
{
if ($length == 0)
return '';
if (mb_strlen($string,'UTF-8') > $length) {
$length -= min($length, mb_strlen($etc,'UTF-8'));
if (!$break_words && !$middle) {
$string = preg_replace('/\s+?(\S+)?$/', '', mb_substr($string, 0, $length+1,'UTF-8'));
}
if(!$middle) {
return mb_substr($string, 0, $length,'UTF-8') . $etc;
} else {
return mb_substr($string, 0, $length/2,'UTF-8') . $etc . mb_substr($string, -$length/2,'UTF-8');
}
} else {
return $string;
}
}
$break_words = false, $middle = false)
{
if ($length == 0)
return '';
if (mb_strlen($string,'UTF-8') > $length) {
$length -= min($length, mb_strlen($etc,'UTF-8'));
if (!$break_words && !$middle) {
$string = preg_replace('/\s+?(\S+)?$/', '', mb_substr($string, 0, $length+1,'UTF-8'));
}
if(!$middle) {
return mb_substr($string, 0, $length,'UTF-8') . $etc;
} else {
return mb_substr($string, 0, $length/2,'UTF-8') . $etc . mb_substr($string, -$length/2,'UTF-8');
}
} else {
return $string;
}
}
放到目录plugins_ext下, 然后在开头加上:
$smarty->plugins_dir = array("plugins", "plugins_ext");
遇到UTF-8的中文就可以使用utruncate做同样的操作了。