Let CodeIgniter's ellipsize () support the Chinese truncation method. The TextHelper of CodeIgniter has an ellipsize () method, which is used to filter HTML tags and intercept text. However, it is particularly difficult to support Chinese characters. The Text Helper method with garbled characters out of CodeIgniter has an ellipsize () method, which is used to filter HTML tags and intercept Text. However, it is particularly difficult to support Chinese characters, and garbled characters appear in Chinese.
Some netizens modified function ellipsize () to support Chinese characters:
In CI 2.1.3, modify the ci_2.1.3 \ system \ helpers \ text_helper.php file
The code is as follows:
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;
}
This code replaces substr and strlen with mb_substr and mb_strlen.In this way, Chinese truncation is supported.
Javashelper has an ellipsize () method, which is used to filter HTML tags and intercept text. However, it is particularly difficult to support Chinese characters. garbled characters are generated when used in Chinese...