时时商务社区

标题: 让CodeIgniter的ellipsize()支持中文截断的方法 [打印本页]

作者: bewin83    时间: 2018-2-14 05:57

            CodeIgniter的Text Helper有一个ellipsize()方法,用来过滤HTML标签并且截断文字十分好用。但是它对中文支持的特别不好,在中文中使用就有乱码出现。
下面有网友将function ellipsize()进行了修改,使得它支持中文:
在CI 2.1.3版本中,修改ci_2.1.3\system\helpers\text_helper.php 文件
[U]复制代码[/U] 代码如下:function ellipsize($codepage = 'UTF-8',
                   $str, $max_length, $position = 1, $ellipsis = '…')
{
    // Strip tags
    $str = trim(strip_tags($str));
    // Is the string long enough to ellipsize?
    if (mb_strlen($str, $codepage)  1) ? 1 : $position;
    if ($position === 1)
    {
        $end = mb_substr($str, 0,
            -($max_length - mb_strlen($beg, $codepage)), $codepage);
    }
    else
    {
        $end = mb_substr($str,
            -($max_length - mb_strlen($beg, $codepage)), $max_length, $codepage);
    }
    return $beg.$ellipsis.$end;
}
这段代码主要将substr和strlen替换成了mb_substr和mb_strlen,这样就能很好的支持中文截断了。
            
            
您可能感兴趣的文章:
  • codeigniter框架The URI you submitted has disallowed characters错误解决方法
  • CodeIgniter输出中文乱码的两种解决办法
  • Codeigniter框架的更新事务(transaction)BUG及解决方法
  • codeigniter上传图片不能正确识别图片类型问题解决方法
  • Codeigniter中mkdir创建目录遇到权限问题和解决方法
  • CodeIgniter错误mysql_connect(): No such file or directory解决方法
  • Codeigniter购物车类不能添加中文的解决方法
            




    欢迎光临 时时商务社区 (http://bbs.4435.cn/) Powered by Discuz! X3.2