为了便于smarty版本的升级维护,对truncate原函数不做修改而是在plugins下新建一个文件命名为modifier_truncatey。
修改后的函数采用utf-8编码格式,支持中文字符串截取,消除因字符串中包含HTML代码对截取产生的影响。内容如下
<?php
/** * Smarty plugin * @package Smarty * @subpackage plugins */
/** * Smarty truncatey modifier plugin * * Type: modifier<br> * Name: truncatey<br> * 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. * @author Monte Ohrt <monte at ohrt dot com> modify by leijuly * @param string * @param integer * @param string * @param boolean */
function smarty_modifier_truncatey($string, $length = 80, $etc = '...', $count_words = true)
{
mb_internal_encoding("UTF-8");
$string = strip_tags(trim(html_entity_decode($string)));
//去除HTML标签,空格
if ($length == 0) {
return '';
}
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, $info);
if ($count_words) {
$k = 0;
for ($i = 0; $i < count($info[0]); $i++) {
$wordscut .= $info[0][$i];
$k++;
if ($k >= $length) {
return $wordscut . $etc;
}
}
return join('', $info[0]);
}
return join("", array_slice($info[0], 0, $length)) . $etc;
}
/* vim: set expandtab: */
$length = 实际截取字数,不论字母、数字、中文都计为一个“字”
V2 加了个去除空格的过程