Analysis of UrlEncode () and urldecode () URL coding functions in PHP

Source: Internet
Author: User
Tags rar urlencode alphanumeric characters

UrlEncode: Refers to the Web page URL in a code conversion method, the most common is Baidu, Google and other search engines to enter the Chinese query time, generate the URL of the Web page after encode.

There are generally two kinds of urlencode, one is the traditional based on GB2312 encode (Baidu, Yisou, etc.), the other is based on UTF-8 encode (Google, Yahoo and other uses).

UrlDecode: is to restore the URL-encoded string to the image as not encoded.

This tool implements the encode and decode of two different ways:

Encode->%d6%d0%ce%c4 of Chinese-> GB2312

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

UrlEncode in HTML:

In an HTML file encoded as GB2312: http://www.php2.cc/Chinese. rar-> Browser automatically converts to-> Http://www.php2.cc/%D6%D0%CE%C4.rar

Note: Firefox on GB2312 's encode Chinese URL support is not good, because it is the default is UTF-8 code to send the URL, but the ftp://protocol can, I tried, I think this should be a Firefox bug.

In an HTML file encoded as UTF-8: http://www.php2.cc/Chinese. rar-> Browser automatically converts to-> Http://www.php2.cc/%E4%B8%AD%E6%96%87.rar

UrlEncode in PHP:

1 2//gb2312 encode.
3 echo UrlEncode ("Chinese-_."). " \ n "; %d6%d0%ce%c4-_.+
4 echo UrlDecode ("%d6%d0%ce%c4-_."). " \ n "; Chinese-_.
5 echo Rawurlencode ("Chinese-_."). " \ n "; %d6%d0%ce%c4-_.%20
6 echo Rawurldecode ("%d6%d0%ce%c4-_."). " \ n "; Chinese-_.
7?>
Except "-_." All non-alphanumeric characters are replaced with a percent semicolon "%" followed by a two-digit hexadecimal number.

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

If you want to use the UTF-8 encode, there are two ways:
First, the file as a UTF-8 file, directly using UrlEncode, Rawurlencode can be.

Second, using the Mb_convert_encoding function:

1 2 $url = ' http://www.php2.cc/Chinese. rar ';
3 echo UrlEncode (mb_convert_encoding ($url, ' utf-8 ', ' gb2312 ')). " \ n ";
4 echo Rawurlencode (mb_convert_encoding ($url, ' utf-8 ', ' gb2312 ')). " \ n ";
5//http%3a%2f%2fwww.php2.cc%2f%e4%b8%ad%e6%96%87.rar
6?>
Instance:
1 2 function parseURL ($url = "")
3 {
4 $url = Rawurlencode (mb_convert_encoding ($url, ' gb2312 ', ' utf-8 '));
5 $a = array ("%3a", "%2f", "%40");
6 $b = Array (":", "/", "@");
7 $url = Str_replace ($a, $b, $url);
8 return $url;
3 ·
$url = "ftp://ud03:password@www.php2.cc/Chinese/Chinese. rar";
One echo parseURL ($url);
//ftp://ud03:password@www.php2.cc/%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.

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.