PHP urlencode () function-URL encoding conversion

Source: Internet
Author: User
Tags alphanumeric characters

 Understand urlencode:
Urlencode: a pointer to the Chinese characters in the URL of a Web page. The most common method is to enter Chinese Characters in search engines such as Baidu and Google, generate a webpage URL that has passed the encode. Urlencode has two methods: the traditional gb2312-based encode (Baidu, yisou, etc.), and the UTF-8-based encode (Google, Yahoo, etc ). This tool implements encode and decode in two ways respectively.

Chinese-> gb2312 encode-> % D6 % D0 % Ce % c4
English-> UTF-8 encode-> % E4 % B8 % ad % E6 % 96% 87

Urlencode in HTML:
In an HTML file encoded as gb2312,
Http://www.sendnet.cn/rar .rar-> browser automatically converted to-> http://www.sendnet.cn/%D6%D0%CE%C4.rar
Note: Firefox does not support the Chinese URL of gb2312 encode, because it is the UTF-8 code by default to send the URL, but ftp: // protocol can, I tried. I think this is a Firefox bug.

In an HTML file encoded as a UTF-8,
Http://www.sendnet.cn/rar .rar-> browser automatically converted to-> http://www.sendnet.cn//%E4%B8%AD%E6%96%87.rar

Urlencode in PHP:

// Gb2312 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 -_.
?>

All non-alphanumeric characters except-_. will be replaced with a semicolon (%) followed by two hexadecimal numbers.
The difference between urlencode and rawurlencode:
Urlencode encodes a space into a plus sign (+)
Rawurlencode encodes a space into a plus sign (% 20)

If you want to use the UTF-8's encode, there are two ways:
1. Save the file as a UTF-8 file, directly use urlencode, rawurlencode.
2. Use the mb_convert_encoding function.

$ Url = 'HTTP: // www.sendnet.cn/ .rar ';
Echo urlencode (mb_convert_encoding ($ URL, 'utf-8', 'gb2312 '). "\ n ";
Echo rawurlencode (mb_convert_encoding ($ URL, 'utf-8', 'gb2312 '). "\ n ";
// Http%3a%2f%2fwww.sendnet.cn%2f%e4%b8%ad%e6%96%87.rar
?>

Instance:

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: // sendnet: password@ftp.sendnet.cn/Chinese/ .rar ";
Echo parseurl ($ URL );
// Ftp: // sendnet: password@ftp.sendnet.cn/% D6 % D0 % Ce % c4/127d6%d0%ce%c4.rar
?>

Urlencode in javascript:

% E4 % B8 % ad % E6 % 96% 87-_. % 20% E4 % B8 % ad % E6 % 96% 87-_. % 20

Encodeuri does not encode the following characters: ":", "/", ";", "?" And.

Http://www.sendnet.cn/%E4%B8%AD%E6%96%87.rar
Http%3a%2f%2fwww.sendnet.cn%2f%e4%b8%ad%e6%96%87.rar

Online urlencode tool:

Http://tool.chinaz.com/Tools/URLEncode.aspx

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.