Use the built-in php method mb_substr to intercept Chinese characters without garbled characters. it is very easy to use. For more information, see.
GBK encoding truncation example
The code is as follows:
$ Str = 'who am I '; // A gbk-encoded string
Echo mb_substr ($ str, 0, 1, 'gbk'); // output me
The mb_substr method has one more parameter than the substr method to specify string encoding.
UTF-8 code truncation example
[Code]
$ Str = 'who am abc '; // UTF-8 encoded string
Echo mb_substr ($ str, 0, 2, 'utf-8'); // output
[/Code
There is no problem between Chinese and English mixing.
Reminder
Pay attention to the PHP file encoding and the encoding when displaying the webpage.
To use this mb_substr method, you need to know the encoding of the string in advance. if you do not know the encoding, you need to judge it. the mbstring library also provides mb_check_encoding to check the encoding of the string, but it is not complete yet.