让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,这样就能很好的支持中文截断了。
热门推荐
10 五一公司放假祝福语简短
11 追星女孩的祝福语简短
12 给长辈端午祝福语 简短
13 生日祝福语有诗意简短
14 团圆日祝福语简短
15 中秋商务祝福语简短最新
16 温暖文艺简短祝福语短句
17 男孩上学祝福语简短的
18 入住酒店文案祝福语简短