Home>Article>Backend Development> PHP intercepts string without garbled characters
GBK encoding interception example
$str = '我是谁'; //gbk编码的字符串 echo mb_substr($str, 0, 1, 'gbk'); //输出 我
The mb_substr method has one more parameter than substr, which is used to specify the string encoding.
utf-8 encoding interception example
$str = '我abc是谁'; //utf-8编码的字符串 echo mb_substr($str, 0, 2, 'utf-8'); //输出 我a
There is no problem mixing Chinese and English.
Note:
1. When using, pay attention to the encoding of the php file and the encoding when displaying the web page.
2. 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 providesmb_check_encoding
to check the string encoding.
Recommended tutorial:PHP video tutorial
The above is the detailed content of PHP intercepts string without garbled characters. For more information, please follow other related articles on the PHP Chinese website!