Home >Backend Development >PHP Problem >What should I do if php intercepts garbled Chinese strings?
Solution to php intercepting garbled Chinese strings: 1. Use GBK encoding to intercept, the code is [echo mb_substr($str, 1, 'gbk')]; 2. Use [utf-8] encoding to intercept , the code is [echo mb_substr($str, , 'utf-8'].
Solution to php intercepting garbled Chinese strings:
1. GBK encoding interception example
$str = '我是谁'; //gbk编码的字符串 echo mb_substr($str, 0, 1, 'gbk'); //输出 我
mb_substr
The method has one more parameter than substr, which is used to specify the string encoding.
2. UTF-8 encoding interception example
[code] $str = '我abc是谁'; //utf-8编码的字符串 echo mb_substr($str, 0, 2, 'utf-8'); //输出 我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 the web page is displayed.
To use this mb_substr
method, you need to know the encoding of the string in advance. If you don’t know the encoding, just Need to judge, the mbstring library also provides mb_check_encoding
to check the string encoding, but it is not perfect yet.
Related learning recommendations: PHP programming from entry to proficiency
The above is the detailed content of What should I do if php intercepts garbled Chinese strings?. For more information, please follow other related articles on the PHP Chinese website!