扩展smarty 截取方法truncate支持中文
时间:2008-06-03
来源:互联网
/Smarty/plugins/modifier.truncate_cn.php
复制内容到剪贴板
function smarty_modifier_truncate_cn($string, $length = 80, $code = '', $etc = '')
{
if ($length == 0)
return '';
if($code == 'UTF-8'){
$pa = "/[\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]/";
}
else{
$pa = "/[\x01-\x7f]|[\xa1-\xff][\xa1-\xff]/";
}
preg_match_all($pa, $string, $t_string);
if(count($t_string[0]) > $length)
return join('', array_slice($t_string[0], 0, $length)).$etc;
return join('', array_slice($t_string[0], 0, $length));
}
?>
代码:
<?function smarty_modifier_truncate_cn($string, $length = 80, $code = '', $etc = '')
{
if ($length == 0)
return '';
if($code == 'UTF-8'){
$pa = "/[\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]/";
}
else{
$pa = "/[\x01-\x7f]|[\xa1-\xff][\xa1-\xff]/";
}
preg_match_all($pa, $string, $t_string);
if(count($t_string[0]) > $length)
return join('', array_slice($t_string[0], 0, $length)).$etc;
return join('', array_slice($t_string[0], 0, $length));
}
?>
作者: wz_910 发布时间: 2008-06-03
支持,我早就知道了其实!

作者: kaixin99 发布时间: 2008-06-03
谁来解释一下
作者: linvo 发布时间: 2008-06-04
看到正则 我就晕

作者: phpcaicai 发布时间: 2008-06-04
学习下!
作者: 从头再来 发布时间: 2008-06-18
贴上一段我同事两年前写的函数。
复制PHP内容到剪贴板
$break_words = false, $middle = false)
{
if ($length == 0)
return '';
if (mb_strlen($string, $encoded) > $length) {
$length -= mb_strlen($etc, $encoded);
if (!$break_words && !$middle) {
$string = preg_replace('/\s+?(\S+)?$/', '', mb_substr($string, 0, $length+1, $encoded));
}
if(!$middle) {
return mb_substr($string, 0, $length, $encoded).$etc;
} else {
return mb_substr($string, 0, $length/2, $encoded) . $etc . mb_substr($string, -$length/2, $encoded);
}
} else {
return $string;
}
}
PHP代码:
function smarty_modifier_truncateex($string, $length = 80, $encoded = "utf-8", $etc = '...',$break_words = false, $middle = false)
{
if ($length == 0)
return '';
if (mb_strlen($string, $encoded) > $length) {
$length -= mb_strlen($etc, $encoded);
if (!$break_words && !$middle) {
$string = preg_replace('/\s+?(\S+)?$/', '', mb_substr($string, 0, $length+1, $encoded));
}
if(!$middle) {
return mb_substr($string, 0, $length, $encoded).$etc;
} else {
return mb_substr($string, 0, $length/2, $encoded) . $etc . mb_substr($string, -$length/2, $encoded);
}
} else {
return $string;
}
}
作者: 欧阳 发布时间: 2008-07-22
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28