Differences between urlencode and rawurlencode in PHP

Source: Internet
Author: User
Tags form post alphanumeric characters

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.

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.