Get the App
SLTechnology News&Howtos  ›  Development  › 

How to make ellipsize () of CodeIgniter support Chinese truncation

Shulou Source: shulou.com Published: 2022-06-03 16:33:18 09月17日 Update

This article mainly shows you "how to make CodeIgniter's ellipsize () support Chinese truncation". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to make CodeIgniter's ellipsize () support Chinese truncation".

CodeIgniter's Text Helper has an ellipsize () method that is useful for filtering HTML tags and truncating text. However, it does not support Chinese very well, and garbled codes appear when used in Chinese.

The following netizens have modified function ellipsize () to make it support Chinese:

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

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) 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, so it can support Chinese truncation very well.

The above is all the contents of the article "how to make CodeIgniter's ellipsize () support Chinese truncation". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Chinese support content article study help bad random code code file text method easy to understand more organization label version knowledge editing netizens Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Redmi Linux Shulou Information Microsoft