Let CodeIgniter ellipsize () support the Chinese truncation method _php instance

Source: Internet
Author: User
Tags codeigniter

CodeIgniter's Text helper has a ellipsize () method for filtering HTML tags and truncating text. But it is particularly bad for Chinese support, in the use of Chinese is garbled.

Below has the netizen to make the function ellipsize () to carry on the modification, causes it to support the Chinese:

In the CI 2.1.3 version, modify the ci_2.1.3\system\helpers\text_helper.php file

Copy Code code 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 mainly replaces substr and strlen with Mb_substr and Mb_strlen, which will support the truncation of the Chinese language very well.

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.