URLEncode: refers to an encoding and conversion method for Chinese characters in web page URLs. The most common one is that when a Chinese query is entered in search engines such as Baidu and Google, an encoded web page URL is generated.
There are generally two methods of URLEncode, one is the traditional Encode based on GB2312 (used by Baidu, Yisou, etc.), and the other is based on UTF-8 Encode (used by Google, Yahoo, etc.).
This tool implements two methods of Encode and Decode:
Chinese-> Encode of GB2312 -> %D6%D0%CE%C4
Chinese-> Encode of UTF-8 -> %E4 %B8%AD%E6%96%87
URLEncode in HTML:
In the html file encoded as GB2312: http://s.jb51.net/中文.rar -> The browser automatically converts to -> ; http://s.jb51.net/%D6%D0%CE%C4.rar
Note: Firefox does not support the Chinese URL of GB2312 Encode because it sends the URL in UTF-8 encoding by default, but The ftp:// protocol works, I tried it, and I think this is a Firefox bug.
In the html file encoded as UTF-8: http://s.jb51.net/中文.rar -> The browser automatically converts to -> http://s.jb51.net/%E4%B8 %AD%E6%96%87.rar
URLEncode in PHP:
<?php //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"; //中文-_. ?>
All non-alphanumeric characters except "-_." will be replaced with a percent sign "%" followed by two digits of sixteen Base number.
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:
1. Save the file as a UTF-8 file and use urlencode or rawurlencode directly.
2. Use the mb_convert_encoding function:
<?php $url = 'http://s.jb51.net/中文.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.jb51.net%2F%E4%B8%AD%E6%96%87.rar ?>
Example:
<?php 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.jb51.net/中文/中文.rar"; echo parseurl($url); //ftp://ud03:password@s.jb51.net/%D6%D0%CE%C4/%D6%D0%CE%C4.rar ?>
URLEncode in JavaScript:
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.
The above introduces the detailed explanation of URL encoding using the urlencode function in PHP, including the content of urlencode. I hope it will be helpful to friends who are interested in PHP tutorials.