Home  >  Article  >  Backend Development  >  Solution to the problem of Chinese garbled characters in PHP URL_PHP Tutorial

Solution to the problem of Chinese garbled characters in PHP URL_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:43:07990browse

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"; //中文-_.
?>
//Encode of GB2312

echo urlencode("中文-_. ")."n"; //%D6%D0%CE%C4-_.+
echo urldecode("%D6%D0%CE%C4-_. ")."n"; //Chinese-_.

echo rawurlencode("中文-_. ")."n"; //%D6%D0%CE%C4-_.%20

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
?>
1. Save the file as a UTF-8 file, just use urlencode or rawurlencode directly. 2. Use the mb_convert_encoding function:
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:

URLEncode in JavaScript:
The code is as follows
 代码如下 复制代码
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;
}
$url="ftp://ud03:password@s./中文/中文.rar";
echo parseurl($url);
//ftp://ud03:password@s./%D6%D0%CE%C4/%D6%D0%CE%C4.rar
?>
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;

}
$url="ftp://ud03:password@s./中文/中文.rar";

echo parseurl($url);

//ftp://ud03:password@s./%D6%D0%CE%C4/%D6%D0%CE%C4.rar

?>

 代码如下 复制代码


if (preg_match("/^([".chr(228)."-".chr(233)."]{1} [".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1})+$/",$msg)) //如果$msg是UTF-8编码
{
$msg = iconv("UTF-8","GB2312",$msg); //将 $msg 由UTF-8编码转为GB2312编码
}

For example: %E4%B8%AD%E6%96%87-_.%20%E4%B8%AD%E6%96%87-_.%20

encodeURI does not encode the following characters: ":", "/", ";", "?", "@" and other special characters. For example: /%E4%B8%AD%E6%96%87.rarhttp%3A%2F%2Fs.%2F%E4%B8%AD%E6%96%87.rar It seems that the only way to process Chinese is to use the urlencode() method. You need to encode it before passing it, and then solve it after passing it. Then use the following two functions: Encoding: ".urlencode('Chinese'). ", decoding: ".urldecode('Chinese').", the Chinese in brackets are the characters passed. As follows: The delivery page is first encoded by 04ie.com: td.php?id=".urlencode('Chinese').", and the page is accepted for decoding: urldecode(id).". Attach a function later
The code is as follows Copy code
if (preg_match("/^([".chr(228)."-".chr(233)."]{1} [".chr(128)."-".chr(191)."]{ 1}[".chr(128)."-".chr(191)."]{1})+$/",$msg)) //If $msg is UTF-8 encoding
{
$msg = iconv("UTF-8","GB2312",$msg); //Convert $msg from UTF-8 encoding to GB2312 encoding } http://www.bkjia.com/PHPjc/633202.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633202.htmlTechArticleIf you are using apache or linux system, then the problem of Chinese garbled url is very common, let me explain below Let me introduce to you the solution to the problem of Chinese garbled characters in URL transmission in php. I hope...
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