随笔 - 47, 文章 - 10, 评论 - 8, 引用 - 0
数据加载中……

Smarty截取中文扩展,支持UTF-8和GB

 

 1function smarty_modifier_truncate_cn($string, $length = 80, $code = 'UTF-8', $etc = '')
 2{
 3    if ($length == 0)
 4        return '';
 5    if($code == 'UTF-8'){
 6        $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]/";
 7    }
 8    else{
 9        $pa = "/[\x01-\x7f]|[\xa1-\xff][\xa1-\xff]/";
10    }
11    preg_match_all($pa, $string, $t_string);
12    if(count($t_string[0]) > $length)
13        return join('', array_slice($t_string[0], 0, $length)).$etc;
14    return join('', array_slice($t_string[0], 0, $length));
15}

以下代码保存为ascii格式
 1<html>
 2<head>
 3<title>Truncate 测试</title>
 4<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
 5</head>
 6<body>
 7{{$string}}<br>
 8{{$string|truncate_cn:15:"":""}}<br>
 9</body>
10</html>


以下代码保存为:UTF-8格式
 1<html>
 2<head>
 3<title>Truncate 测试</title>
 4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 5</head>
 6<body>
 7{{$string}}<br>
 8{{$string|truncate_cn:15:"UTF-8":""}}<br>
 9</body>
10</html>

posted on 2007-11-20 14:34 编程之道 阅读(1025) 评论(0)  编辑 收藏 引用 所属分类: web编程开发相关


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理