Home > Backend Development > PHP Problem > What should I do if php intercepts garbled Chinese strings?

What should I do if php intercepts garbled Chinese strings?

coldplay.xixi
Release: 2023-03-02 21:52:01
Original
3061 people have browsed it

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'].

What should I do if php intercepts garbled Chinese strings?

Solution to php intercepting garbled Chinese strings:

1. GBK encoding interception example

$str = '我是谁';  //gbk编码的字符串
echo mb_substr($str, 0, 1, 'gbk'); //输出 我
Copy after login

mb_substrThe 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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template