Php Chinese string truncation method parsing ,. The Chinese character string truncation method of php is parsed. garbled characters may occur when the substr function of PHP is used to intercept Chinese characters. The main reason is that substr may generate a Chinese character into two halves. Solution: parse the Chinese string truncation method in php,
Using the PHP function substr to intercept Chinese characters may cause garbled characters, mainly because substr may generate a Chinese character "sawing" into two halves.
Solution:
1. use mb_substr of the mbstring Extension Library to intercept the database without garbled characters.
2. write the truncation function by yourself, but it is not as efficient as using the mbstring Extension Library.
3. if you only want to output the intercepted string, use the following method: substr ($ str, 0, 30). chr (0 ).
The substr () function can split text. However, if the text to be split contains Chinese characters, you can use the mb_substr ()/mb_strcut function, mb_substr () the usage of/mb_strcut is similar to that of substr (), but an additional parameter must be added at the end of mb_substr ()/mb_strcut to set the character string encoding. However, php_mbstring.dll is not enabled on the server, in php. ini opens php_mbstring.dll.
For example:
Output: in this way, my words
Output:
Mb_substr is used to split characters by words, while mb_strcut is used to split characters by bytes, but it does not produce half a character.
PHP implementation of Chinese string interception without garbled characters
(127) {$ content. = substr ($ str, $ I, 2); $ I ++; // www.jbxue.com} else {$ content. = substr ($ str, $ I, 1);} if (++ $ count = $ mylen) {break ;}} echo $ content ;} $ str = "34 People's Republic of China 56"; substr_CN ($ str, 3); // output in 34?>
References:
Example of the substr string truncation function in php
Usage of the substr string truncation function in php
Supports Chinese php string truncation functions
Http://www.bkjia.com/PHPjc/886486.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/886486.htmlTechArticlephp Chinese string truncation method analysis, using the PHP function substr to intercept Chinese characters may appear garbled, mainly substr may be hard to generate a Chinese character into two half. Solution...