两种让smarty truncate变量调节器支持中文的方法

| No Comments | No TrackBacks
1.使用Multi-Byte String Functions ,需要服务器安装相应模块
 
新建文件: modifier.mb_truncate.php
 
function smarty_modifier_mb_truncate($string, $length = 80, $etc = '...', $middle = false)
{
    if ($length == 0)
        return '';
    if (mb_strlen($string, 'UTF-8') > $length) {
        $length -= mb_strlen($etc);
        if(!$middle) {
            return mb_substr($string, 0, $length).$etc;
        } else {
            return mb_substr($string, 0, $length/2) . $etc . mb_substr($string, -$length/2, $length/2, 'UTF-8');
        }
    } else {
        return $string;
    }
}
 
2.
新建文件: modifier.mb_substring.php
 
 function smarty_modifier_substring($string, $from, $length = null)
{
    preg_match_all('/[\x80-\xff]?./', $string, $match);
    if(is_null($length))
    {
        $result = implode('', array_slice($match[0], $from));
    }
    else
    {
        $result = implode('', array_slice($match[0], $from, $length));
    }
    return $result;
}

No TrackBacks

TrackBack URL: http://www.wujianrong.com/mt-tb.cgi/1643

Leave a comment

About this Entry

This page contains a single entry by kevinwu published on January 25, 2007 4:11 PM.

用 Spring 更好地处理 Struts 动作三种整合 Struts 应用程序与 Spring 的方式 was the previous entry in this blog.

Hibernate代码生成工具 设计全攻略 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.