Let CodeIgniter's ellipsize () support Chinese truncation method _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.