让CodeIgniter的ellipsize()支持中文截断的方法
CodeIgniter的TextHelper有一个ellipsize()方法,用来过滤HTML标签并且截断文字十分好用。但是它对中文支持的特别不好,在中文中使用就有乱码出现。
下面有网友将functionellipsize()进行了修改,使得它支持中文:
在CI2.1.3版本中,修改ci_2.1.3\system\helpers\text_helper.php文件
functionellipsize($codepage='UTF-8',
$str,$max_length,$position=1,$ellipsis='…')
{
//Striptags
$str=trim(strip_tags($str));
//Isthestringlongenoughtoellipsize?
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,这样就能很好的支持中文截断了。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下