Solution for php to obtain garbled get parameters: 1. Process the encoding through "$str=iconv("gb2312","utf-8",$str);"; 2. Through "mb_convert_encoding($ str,"utf-8", "gb2312");" method.
The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.
In-depth study on PHP receiving GET Chinese parameter garbled code
I believe that many PHPer will encounter such a problem: In # Under the page of ##utf-8, if you directly access the address with Chinese parameters such as test.php?s=test The value of such an address output parameter will be garbled. I searched for relevant information on the search engine, and only some solutions were given, but no one studied the cause of this problem. Today I am writing this article to delve into the cause of this problem. Reason:
First we demonstrate this problem. The test code and running results are as follows.
Code:
Test result:
The code declares that the encoding of the response content is utf-8, the displayed content is indeed garbled.
Please note here thatvar_dumpThe length of the output variable is only4 , obviously, the length of the two Chinese characters must be more than 4 under the utf-8 encoding Bytes
Then let’s take a look at Firefox’s access to this pageurl
##FireFox
will automatically encode the Chinese url, so we can see that the test becomes ����, very Obviously, one word here is two bytes, which is Chinese encoding format such as gb2313, gbk, not utf-8Encoding. (Recommended: "PHP Video Tutorial")
If we switch the encoding of the page to
gbk, Chinese parameters will be displayed normally, see the picture below
At this time, an interesting question arises: How can parameters like the Chinese label of emlog No garbled characters? After multiple tests, I found a small difference: emlogThe link to the Chinese parameters is generated on the page, and in our test above, we entered it directly in the address bar by hand, If we enter it directly, for examplehttp://be-evil.org/?tag=originalFor such a link, the program will also prompt that the tag cannot be found The test code is as follows:
#Test result, normal display:
Please pay attention to the url code marked in the red box in the picture above. The two characters in this test are composed of It consists of 6 bytes instead of the previous 2 bytes, thus indicating that the Chinese parameters have correctly become utf-8 encoding. So, what causes this problem? The answer is the browser default encoding We are making trouble, we are all using the Chinese system, the browser default encoding The encoding will naturally be set to localization. For example, the default encoding of IE and FireFox on my own computer is ## For the #gb series, please see the picture below: IE Default setting: Default settings for ##Firefox
## Because of this setting, the browser will default to url when requesting the url entered by the user. The Chinese is sent in the default encoding format instead of the encoding format of the page, which is why the links with Chinese in the page are normal but the links we input manually will be garbled. In the same way, if we adjust the default encoding of the browser to utf-8, then the Chinese in url will be utf-8Encoding. In addition to the above, there are also the following situations where this will happen: If the address generated by the gbk encoded page links to utf-8## The page of #, the Chinese of the gbk page is encoded in the format of gbk and sent to the next page, then utf-8There will definitely be garbled characters after encoding is received. IIS#urlRewrite module, rewrite The written Chinese encoding is also gbk. If your page is encoded in utf-8, then the rewriting parameters will be invalid. In these cases, we need to use the built-in transcoding function of php Let’s deal with the encoding problem: Solution1: $str =iconv("gb2312","utf-8",$str); ## Scheme 2: mb_convert_encoding($str, "utf-8", "gb2312"); I hope this article will help those who are scratching their heads due to encoding problems PHPer We are helpful :)
The above is the detailed content of What should I do if php obtains garbled get parameters?. For more information, please follow other related articles on the PHP Chinese website!