Differences between php urlencode and rawurlencode

Source: Internet
Author: User
Tags form post alphanumeric characters

In php, rawurlencode and rawurlencode both encode characters. Next I will introduce the differences between urlencode and rawurlencode. For more information, see.

Urlencode is used to encode a string. replace all non-alphanumeric characters except "-_" in the original string with a semicolon (%) followed by two hexadecimal numbers, however, note that due to historical reasons, spaces are replaced with the + number. Rawurlencode is actually used to encode strings like urlencode. The only difference is that it uses RFC1738 encoding, that is, it will replace the space with % 20.

The corresponding decoding functions are urldecode and rawurldecode. Refer to the instructions on the official website. Any % ##, plus sign ('+') in the encoded string given by urldecode is decoded into a space character; the semicolon (%) in the rawurldecode decoding character string is followed by two hexadecimal values. There are two differences: first, urldecode decoding decodes any two characters after the percent sign (%). For example, % MN can also be decoded but fails; rawurldecode only decodes the last two hexadecimal (0-9A-F) characters after the percent sign (%). Second, urldecode decodes the plus sign as a space.

Let's look at the official introduction of the two functions in php.

Urlencode-encode a URL string

Report a bug description
String urlencode (string $ str)
This function allows you to encode a string and use it in the URL request section. It also allows you to pass variables to the next page.

Report a bug Parameters

Str
The string to be encoded.

Report a bug Return Value
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 (+.

The Code is as follows: Copy code

Example #1 urlencode () Example

<? Php
Echo '<a href = "mycgi? Foo = ', urlencode ($ userinput),' "> ';
?>


Example #2 Examples of urlencode () and htmlentities ()

<? Php
$ Query_string = 'foo = '. urlencode ($ foo).' & bar = '. urlencode ($ bar );
Echo '<a href = "mycgi? '. Htmlentities ($ query_string).' "> ';
?>

Rawurlencode-encode the URL according to RFC 1738

Report a bug description
String rawurlencode (string $ str)
Encode the specified characters according to» RFC 3986.

Report a bug Parameters

Str
The URL to be encoded.


Report a bug Return Value
Returns a string. All non-alphanumeric characters except-_. In this string will be replaced with a semicolon (%) followed by two hexadecimal numbers. This is the encoding described in RFC 3986 to protect the original characters from being interpreted as special URL delimiters, at the same time, the URL format is protected to prevent the transmitted media (such as some mail systems) from interfering with character conversion.

Example

The Code is as follows: Copy code

Example #1 include a password in the FTP URL

<? Php
Echo '<a href = "ftp: // user:', rawurlencode ('foo @ + % /'),
'@ Ftp.example.com/x.txt "> ';
?>
The above routine will output:

<A href = "ftp: // user: foo % 20% 40% 2B % 25% 2F@ftp.example.com/x.txt">

Based on the description of the decoding function, we can infer that urldecode can be used to decode urlencode or rawurlencode. However, if the original string contains spaces, the string decoded using rawurlencode is different from the original string.

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.