How to correctly count Chinese words in PHP? This is a problem that has plagued me for a long time, there are many functions in PHP that can calculate the length of a string, such as the following example, respectively, using the
Strlen
Mb_strlen
Mb_strwidth
This three function to test the length of the statistical string, to see how to count Chinese into several bytes:
[Code]echo strlen ("Hello abc"). ""; # Output 9echo mb_strlen ("Hello abc", ' UTF-8 '). "; # Output 5echo mb_strwidth ("Hello abc"). ""; #输出 7
From the above test, we can see:
Strlen
The Chinese characters are counted as 3 bytes,
Mb_strlen
Whether Chinese or English, it counts 1 bytes, and
Mb_strwidth
The Chinese is counted as 2 bytes, so
Mb_strwidth
is what we want: Chinese 2 bytes, English 1 bytes .
It is also recommended to use the same intercept string
Mb_strimwidth
, is also according to the Chinese 2 byte, the English 1 byte method calculates after, and if the word number exceeds the interception request, this function can also automatically add ' ... ' in the last side.
[Code]mb_strimwidth ($post _excerpt,0,240, ' ... ', ' utf-8 ');
Note that the last addition of the ' Utf-8 ' encoding parameter, can avoid the problem of Chinese truncation garbled.