找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 228|回复: 0
打印 上一主题 下一主题

让CodeIgniter的ellipsize()支持中文截断的方法

[复制链接]

2500

主题

2513

帖子

7520

积分

论坛元老

Rank: 8Rank: 8

积分
7520
跳转到指定楼层
楼主
发表于 2018-2-14 05:57:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            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购物车类不能添加中文的解决方法
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端