If you are using an Apache or Linux system, the problem of Chinese garbled URLs is very common. The following editor will introduce to you the solution to the problem of Chinese garbled URLs in PHP. I hope this method will be helpful to all students. .
When using ?id="中文" to pass Chinese parameters, garbled characters appear. This is the result of secondary transcoding. In php, Chinese cannot be directly transmitted in the URL. For this I have always been dissatisfied and have no choice. Why don't we have a solution? I don't know if this problem also occurs in other languages.
For what is said online about adding header(“content-type:text/html;charset=utf-8″); on the homepage, and the solution of setting the database page, etc. to utf8, it is completely invalid and always The Chinese passed through is garbled.
Although I use the unified utf8 encoding for all 04ie.com site PHP, it is always garbled when passed through. Later, I tested several browsers and found that 360 can be passed through, but IE cannot. Later, I used $msg = iconv('gbk','utf-8′,$_GET["msg"]); After converting and testing several browsers, most of them still display garbled characters.
Finally, to summarize, for the past value of GET[], Chinese cannot be directly transmitted in the URL. If it must be transmitted, use the urlencode() method to process Chinese. I don’t know what to do with POST[], I haven’t done any experiments yet.
Come to the PHP manual to check the use of urlencode():
urlencode() This function encodes a string into URL. For example, spaces will become plus signs. The form data transmission in Homepage is to encode it with urlencode and then send it out
I see, I asked you why there is no problem when submitted from the form, but the URL passed over is garbled
This tool implements two methods of Encode and Decode respectively:
Chinese -> Encode of GB2312 -> %D6%D0%CE%C4
Chinese -> UTF-8 Encode -> %E4%B8%AD%E6%96%87
URLEncode in Html:
In the html file encoded as GB2312: /中文.rar -> The browser automatically converts it to -> /%D6%D0%CE%C4.rar
Note: Firefox does not support Chinese URLs of GB2312 Encode because it sends URLs in UTF-8 encoding by default, but the ftp:// protocol is fine. I tried it. I think this should be regarded as a Firefox bug. .
In the html file encoded as UTF-8: /中文.rar -> The browser automatically converts to -> /%E4%B8%AD%E6%96%87.rar
URLEncode in PHP:
The code is as follows | Copy code |
代码如下 | 复制代码 |
//GB2312的Encode echo urlencode("中文-_. ")."n"; //%D6%D0%CE%C4-_.+ echo urldecode("%D6%D0%CE%C4-_. ")."n"; //中文-_. echo rawurlencode("中文-_. ")."n"; //%D6%D0%CE%C4-_.%20 echo rawurldecode("%D6%D0%CE%C4-_. ")."n"; //中文-_. ?> |
echo urlencode("中文-_. ")."n"; //%D6%D0%CE%C4-_.+
echo urldecode("%D6%D0%CE%C4-_. ")."n"; //Chinese-_.
echo rawurldecode("%D6%D0%CE%C4-_. ")."n"; //Chinese-_.
?> All non-alphanumeric characters except "-_." will be replaced with a percent sign "%" followed by two hexadecimal digits.The difference between urlencode and rawurlencode: urlencode encodes spaces as a plus sign "+", and rawurlencode encodes spaces as a plus sign "%20".
If you want to use UTF-8 Encode, there are two methods:
代码如下 | 复制代码 |
$url = '/中文.rar'; echo urlencode(mb_convert_encoding($url, 'utf-8', 'gb2312'))."n"; echo rawurlencode(mb_convert_encoding($url, 'utf-8', 'gb2312'))."n"; //http%3A%2F%2Fs.%2F%E4%B8%AD%E6%96%87.rar ?> |
The code is as follows | Copy code |
$url = '/中文.rar'; <🎜> echo urlencode(mb_convert_encoding($url, 'utf-8', 'gb2312'))."n"; <🎜> echo rawurlencode(mb_convert_encoding($url, 'utf-8', 'gb2312'))."n"; <🎜> //http%3A%2F%2Fs.%2F%E4%B8%AD%E6%96%87.rar <🎜> ?> |
Example:
The code is as follows
|
Copy code
|
||||
function parseurl($url="") { $url = rawurlencode(mb_convert_encoding($url, 'gb2312', 'utf-8'));$a = array("%3A", "%2F", "%40"); $b = array(":", "/", "@");$url = str_replace($a, $b, $url); return $url;
} //ftp://ud03:password@s./%D6%D0%CE%C4/%D6%D0%CE%C4.rar ?>
|