PHP URL Pass Chinese garbled problem Solution _php Tutorial

Source: Internet
Author: User
Tags rar alphanumeric characters
If you use an Apache or Linux system then the URL Chinese garbled This problem is very common things, the following small part to introduce you to the URL in PHP to pass the Chinese garbled problem solution, I hope this method for the students have help OH.

When using the id= "Chinese" to pass the Chinese parameter, there is garbled, this is after two transcoding results, wherein, in PHP, the URL is not directly transmitted in Chinese, for this, has been dissatisfied, no way ah, who let us not a set of solutions, I don't know if other languages have such problems.

For the online said on the homepage add header ("Content-type:text/html;charset=utf-8″"); Such code, as well as the database page and so on set to UTF8 solution is simply invalid, always pass over the Chinese is garbled.

Although all 04ie.com site PHP I use a unified UTF8 encoding, but always pass the past is garbled, and later tested several browsers, found that 360 can pass past, and IE not, and then use $msg = Iconv (' GBK ', ' utf-8′,$_get[' m SG "]); After the conversion, testing a few browsers, mostly still show garbled problem.

Finally, it concludes that for get[] the past value, the URL can not be transmitted directly in Chinese, if it must be transmitted, the UrlEncode () method to handle Chinese. For post[] Do not know how, for the moment has not done experiments.


Then check the PHP manual for the use of UrlEncode ():

UrlEncode () This function encodes a string as a URL. For example, a space will become a plus sign. Homepage in the form data transfer is to use UrlEncode encoding and then send out

So, I said, why is it not a problem to submit it from the form form, but the URL passed it is garbled.


This tool implements encode and decode in two ways, respectively:

Encode,%D6%D0%CE%C4, GB2312, Chinese

Encode,%e4%b8%ad%e6%96%87, UTF-8, Chinese

UrlEncode in HTML:

HTML file encoded as GB2312:/Chinese. RAR---browser automatically converted to/%d6%d0%ce%c4.rar

Note: Firefox is not good for the Chinese URL of GB2312 encode because it defaults to UTF-8 encoding send URL, but ftp://protocol can, I tried, I think this should be a Firefox bug.

HTML file encoded as UTF-8:/Chinese. RAR---browser automatically converted to/%e4%b8%ad%e6%96%87.rar

UrlEncode in PHP:

The code is as follows Copy Code
GB2312 's Encode
Echo UrlEncode ("Chinese-_."). " n "; %d6%d0%ce%c4-_.+
Echo UrlDecode ("%d6%d0%ce%c4-_."). " n "; Chinese-_.
Echo Rawurlencode ("Chinese-_."). " n "; %d6%d0%ce%c4-_.%20
Echo Rawurldecode ("%d6%d0%ce%c4-_."). " n "; Chinese-_.
?>


Except for "-_." All non-alphanumeric characters will be replaced with a percent percent "%" followed by a two-digit hexadecimal number.

The difference between UrlEncode and Rawurlencode: UrlEncode encodes a space as a plus "+", and Rawurlencode encodes a space as a plus "%20".

If you want to use UTF-8 's encode, there are two ways:

One, save the file as UTF-8 file, directly use UrlEncode, Rawurlencode can.

Second, use the Mb_convert_encoding function:

The code is as follows Copy Code
$url = '/Chinese. 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
?>


Instance:

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;
}
$url = "ftp://ud03:password@s./Chinese/English. rar";
echo parseURL ($url);
Ftp://ud03:password@s./%d6%d0%ce%c4/%d6%d0%ce%c4.rar
?>


UrlEncode in javascript:

such as:%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.

such as:/%e4%b8%ad%e6%96%87.rarhttp%3a%2f%2fs.%2f%e4%b8%ad%e6%96%87.rar


It seems that only use the UrlEncode () method to deal with the Chinese language, before the delivery of the first to encode, pass the past before the resolution is OK, then use the following two functions: code: ". UrlEncode (' Chinese ').", Decode: ". UrlDecode (' Chinese ')." , the Chinese in parentheses is the character passed.

As follows: The delivery page is first 04ie.com encoded: td.php?id= ". UrlEncode (' Chinese ').", accept the page to decode: UrlDecode (ID). ".

Then a function is attached

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 encoded
{
$msg = Iconv ("UTF-8", "GB2312", $msg); Converting $msg from UTF-8 encoding to GB2312 encoding
}

http://www.bkjia.com/PHPjc/633202.html www.bkjia.com true http://www.bkjia.com/PHPjc/633202.html techarticle If you are using an Apache or Linux system then URL Chinese garbled This problem is very common things, the following small part to introduce you to the URL in PHP to pass the Chinese garbled problem solving method, the Greek ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.