Home>Article>Backend Development> What should I do if Chinese garbled characters appear in the php get request?
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.
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:
The test results are as follows:
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
##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 Next we do another test, the code is as follows:Test result, normal display: 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!