欢迎来到代码驿站!

PHP代码

当前位置:首页 > 软件编程 > PHP代码

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

时间:2021-06-01 08:51:29|栏目:PHP代码|点击:

CodeIgniter的Text Helper有一个ellipsize()方法,用来过滤HTML标签并且截断文字十分好用。但是它对中文支持的特别不好,在中文中使用就有乱码出现。

下面有网友将function ellipsize()进行了修改,使得它支持中文:

在CI 2.1.3版本中,修改ci_2.1.3\system\helpers\text_helper.php 文件

复制代码 代码如下:
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) <= $max_length)
    {
        return $str;
    }

    $beg = mb_substr($str, 0, floor($max_length * $position), $codepage);

    $position = ($position > 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,这样就能很好的支持中文截断了。

上一篇:PHP 已经成熟

栏    目:PHP代码

下一篇:phpinfo的知识点总结

本文标题:让CodeIgniter的ellipsize()支持中文截断的方法

本文地址:http://www.codeinn.net/misctech/132982.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有