In PHP, functions such as urlencode (), urldecode (), rawurlencode (), and rawurldecode () are provided to solve the URL encoding and decoding problem.
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 can be either of the traditional GB2312-based Encode (used by Baidu or Yisou) or UTF-8-based Encode (used by Google or Yahoo ). This article analyzes the two methods of Encode and Decode.
Chinese-> GB2312 Encode-> % D6 % D0 % CE % C4
Chinese-> UTF-8 Encode-> % E4 % B8 % AD % E6 % 96% 87
Urlencode in Html:
In the html file encoded as GB2312:
Http://www.phpernote.com/rar .rar-> browser automatically converted to-> http://www.phpernote.com/%D6%D0%CE%C4.rar
Note: Firefox does not support the Chinese URL of GB2312 Encode, because it uses UTF-8 encoding to send URLs by default, but the ftp: // protocol is acceptable, which should be regarded as a Firefox bug.
In an html file encoded as UTF-8:
Http://www.phpernote.com/rar .rar-> browser automatically converted to-> http://www.phpernote.com/%E4%B8%AD%E6%96%87.rar
Urlencode in PHP:
// GB2312 Encodeecho 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)
In my previous version, the txt file Splitter (online) Code uses urlencode. I have never found this problem. As a result, a serious bug has occurred today, all URLs with spaces cannot be parsed, and split files cannot be downloaded. The rawurlencode () function solves this problem.
To use the UTF-8 Encode, you can use either of the following methods:
1. Save the file as a UTF-8 file and use urlencode and rawurlencode directly.
2. Use the mb_convert_encoding function.
$ Url = 'HTTP: // www.phpernote.com/ .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.huikaiche.com%2F%E4%B8%AD%E6%96%87.rar
Application 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: // yongfu: password@www.huikaiche.com/Chinese .rar "; echo parseurl ($ url); // ftp: // yongfu: password@www.huikaiche.com/% D6 % D0 % CE % C4/%d61_d0%ce%c4.rar
Articles you may be interested in
- Comparison and Analysis of string encoding functions escape, encodeURI, and encodeURIComponent in javascript
- Php string replacement function str_replace is faster than preg_replace
- PHP generates continuous numbers (letters) Array Function range () analysis, PHP lottery program function
- Summary of String Functions in PHP
- Php extracts the birthday date from the ID card number and verifies whether it is a minor.
- Php searches for the existence of a value in the array (in_array (), array_search (), array_key_exists ())
- Use the PHP function memory_get_usage to obtain the current PHP memory consumption for program performance optimization.
- SMTP Error cocould not connect to SMTP host. send fail