How to correctly count the number of Chinese characters in PHP? This is a problem that has troubled me for a long time. There are many functions in PHP that can calculate the length of string. For example, in the following example, three of
strlen
mb_strlen
mb_strwidth
are used respectively. Function to test the length of the statistical string to see how many bytes Chinese characters are calculated:
[code]echo strlen("你好ABC") . ""; # 输出 9 echo mb_strlen("你好ABC", 'UTF-8') . ""; # 输出 5 echo mb_strwidth("你好ABC") . ""; #输出 7
From the above test, we can see:
strlen
convert Chinese characters Counted as 3 bytes,
mb_strlen
regardless of Chinese or English, counted as 1 bytes, and
mb_strwidth
counted as Chinese Into 2 bytes, so
mb_strwidth
is what we want: 2 bytes for Chinese, 1 byte for English.
It is also recommended to use
mb_strimwidth
to intercept strings, which is also calculated according to the 2 bytes in Chinese and 1 byte in English, and if the number of words exceeds the interception requirements, This function can also automatically add '...' at the end.
[code]mb_strimwidth($post_excerpt,0,240,'...','utf-8');
Note that adding the ‘utf-8’ encoding parameter at the end can avoid the problem of garbled Chinese interception.
The above is the detailed content of How to count the number of Chinese characters in php. For more information, please follow other related articles on the PHP Chinese website!