Home >Backend Development >PHP Problem >What should I do if Chinese garbled characters appear in the php get request?

What should I do if Chinese garbled characters appear in the php get request?

藏色散人
藏色散人Original
2020-08-24 09:34:594171browse

php get request Chinese garbled solution: 1. Use [$name = iconv("gbk","utf-8",$name);] method to solve the garbled code; 2. Use [mb_convert_encoding($ name, "utf-8", "gbk");] method.

What should I do if Chinese garbled characters appear in the php get request?

Recommended: "PHP Video Tutorial"

php receives garbled Chinese parameters passed in by the GET method

I recently wrote a simple page and passed in Chinese parameters from the browser (test.php?name=test). No matter how I set the utf-8 page, garbled characters were displayed. I googled it. I found a lot of solutions, but what is the cause of the problem? No one has studied this issue in depth. Out of curiosity, I had to check out what was causing it, and it would also gain me some experience!

First, let’s take a look at the simple test code:

<?php   
header("Content-Type:text/html;charset=UTF-8");   
$name = $_GET[&#39;name&#39;];   
var_dump($name);   
?>

The test results are as follows:

What should I do if Chinese garbled characters appear in the php get request?

The code states that the encoding of the response content is utf-8, and the displayed content is indeed garbled. Please note here that the length of the variable output by var_dump is only 4. Obviously, the length of the two Chinese characters must be more than 4 bytes under utf-8 encoding. Then let's take a look at the url of Firefox to access this page

What should I do if Chinese garbled characters appear in the php get request?

##FireFox will automatically Chinese url encoding, so we can see that the test becomes ����. Obviously, one word here is two bytes, which is Chinese encoding format such as gb2313, gbk, etc., not utf-8 encoding. If we switch the encoding of the page to gbk, the Chinese parameters will display normally, see the picture below

What should I do if Chinese garbled characters appear in the php get request?

Next we do another test, the code is as follows:

<?php   
header("Content-Type:text/html;charset=UTF-8");   
$name = $_GET[&#39;name&#39;];   
var_dump($name);   
?>   
  

Test result, normal display:

What should I do if Chinese garbled characters appear in the php get request?

So, what is it? What causes this problem to occur?

The answer is that the default encoding of the browser is causing trouble. We all use the Chinese system, and the default encoding of the browser will naturally be set to localization. For example, the default encodings of IE and FireFox on my own computer are both gb series.

When the browser requests the url entered by the user, it will send the Chinese in the url in the default encoding format instead of the encoding format of the page. This is why the links with Chinese in the page are normal and we The reason why the manually entered link

is garbled. In the same way, if we adjust the browser's default encoding to utf-8, then the Chinese in the input URL will be encoded in utf-8.

In addition to the above, this situation will occur in the following situations:

If the address generated by the gbk-encoded page is linked to the utf-8 page, the Chinese of the gbk page is in accordance with The format encoding of gbk is transmitted to the next page, so garbled characters will definitely appear after receiving the utf-8 encoding.

The url rewriting module of IIS, the rewritten Chinese encoding is also gbk. If your page is utf-8 encoding, the rewriting parameters will be invalid. In these cases, we need to use PHP's built-in transcoding function to deal with encoding problems:

Option 1:

$name = iconv("gbk","utf-8",$name);

Option 2:

mb_convert_encoding($name, "utf-8", "gbk");

The above is the detailed content of What should I do if Chinese garbled characters appear in the php get request?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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