Analysis of urlencode () and urldecode () URL encoding functions in php [transfer], urlencodeurldecode
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.
There are two URLEncode methods. One is the traditional GB2312-based Encode (Baidu, Yisou, etc ), another is UTF-8-based Encode (Google, Yahoo, etc ).
URLdecode: restores the url-encoded string to unencoded format.
This tool implements two methods of Encode and Decode:
Chinese-> GB2312 Encode-> % D6 % D0 % CE % C4
English-> UTF-8 Encode-> % 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 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 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:
Copy the Code as follows:
1 <? Encode3 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 -_. % 206 echo rawurldecode ("% D6 % D0 % CE % C4 -_. "). "\ n"; // Chinese -_. 7?>
All non-alphanumeric characters except "-_." will be replaced with a semicolon "%" followed by two hexadecimal numbers.
The difference between urlencode and rawurlencode: urlencode encodes the space into the plus sign "+", and rawurlencode encodes the space into the 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.
Ii. Use the mb_convert_encoding function:
Copy the Code as follows:
1 <? Php2 $ url = 'HTTP: // s.jb51.net/ .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%2Fs.jb51.net%2F%E4%B8%AD%E6%96%87.rar 6?>
Instance:
Copy the Code as follows:
1 <? Php 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; 9} 10 $ url = "ftp: // ud03: password@s.jb51.net/Chinese/..rar "; 11 echo parseurl ($ url); 12 // ftp: // ud03: password@s.jb51.net/% D6 % D0 % CE % C4/%D6%D0%CE%C4.rar 13?>
URLEncode in JavaScript:
Example: % E4 % B8 % AD % E6 % 96% 87-_. % 20% E4 % B8 % AD % E6 % 96% 87-_. % 20
EncodeURI does not encode the following characters: ":", "/", ";", "?" And.
Such as: http://s.jb51.net/%E4%B8%AD%E6%96%87.rarhttp%3A%2F%2Fs.jb51.net%2F%E4%B8%AD%E6%96%87.rar