GBK encoding interception example
The mb_substr method has one more parameter than substr, which is used to specify the string encoding.
utf-8 encoding interception example
[code]
$str = 'Who am I abc'; //utf-8 encoded string
echo mb_substr($str, 0, 2, 'utf-8'); // Output my a
[/code
There is no problem mixing Chinese and English.
Friendly reminder
When using it, pay attention to the encoding of the php file and the encoding when displaying the web page.
To use this mb_substr method, you need to know the encoding of the string in advance. If you don’t know the encoding, you need to judge. The mbstring library also provides mb_check_encoding to check the string encoding, but it is not perfect yet.