When I checked the uchome source code yesterday, I found that urlencode was used in some places, and rawurlencode was used in some places. The difference between the two methods is not very clear.CodeTo test.
Save the following code to a PHP file:
<? PHP test_encode ( 'Http: // www.baidu.com? A = SEARCH & K = eclipse' ); Test_encode ( ':/? = &#' ); Test_encode ( 'Chinese' ); Function Test_encode ( $ S ){ Echo "<B> urlencode (' $ S ') </B> = [<B>"; Var_dump ( Urlencode ( $ S )); Echo "</B>] <br/>" ; Echo "<B> rawurlencode (' $ S ') </B> = [<B>" ; Var_dump ( Rawurlencode ( $ S )); Echo "</B>] <br/>" ;}
The above code execution result is as follows:
Urlencode ('HTTP: // www.baidu.com? A = SEARCH & K = eclipse ') = [ String (53) "HTTP % 3A % 2f % 2fwww.baidu.com % 3fa % 3 DSearch % 26 K % 3 declipse" ] Rawurlencode ('HTTP: // www.baidu.com? A = SEARCH & K = eclipse ') = [ String (53) "HTTP % 3A % 2f % 2fwww.baidu.com % 3fa % 3 DSearch % 26 K % 3 declipse" ] Urlencode (':/? = & # ') = [String (19) "% 3A % 2f % 3f % 3d + % 26% 23" ] Rawurlencode (':/? = & # ') = [ String (21) "% 3A % 2f % 3f % 3d % 20% 26% 23" ] Urlencode ('Chinese') = [ String (18) "% E4 % B8 % ad % E6 % 96% 87" ] Rawurlencode ('Chinese') = [ String (18) "% E4 % B8 % ad % E6 % 96% 87"]
From the preceding execution results, we can see that the urlencode and rawurlencode methods have the same results when dealing with letters, numbers, special symbols, and Chinese characters. The only difference is the processing of spaces, urlencode is processed as "+", and rawurlencode is processed as "% 20 ".
Let's take a look at the description of the two functions in PHP manual:
Urlencode: returns a string -_. all other non-alphanumeric characters will be replaced with a semicolon (%) followed by two hexadecimal numbers, and spaces will be encoded as the plus sign (+ ). This encoding method is the same as that for WWW form post data and the same as that for application/X-WWW-form-urlencoded. For historical reasons, this encoding is different from rfc1738 encoding (see rawurlencode () in Space Encoding As the plus sign (+.
Rawurlencode: return a string. All non-alphanumeric characters except-_. In this string are replaced with a semicolon (%) followed by two hexadecimal numbers. This encoding is described in RFC 1738 to protect the original characters from being interpreted as special URL delimiters and protect the URL format to prevent them from being transmitted to media (like some email systems) use character conversion.