The difference between PHP urlencode and Rawurlencode _php tutorial

Source: Internet
Author: User
Tags form post php introduction rfc urlencode alphanumeric characters
In PHP Rawurlencode and Rawurlencode are the characters are encoded, the following I will introduce UrlEncode and rawurlencode the difference, there is a need to understand the friends can refer to.

The purpose of UrlEncode is to encode a string, replacing all non-alphanumeric characters in the original string with all but "-_" as a percent sign (%) followed by a two-digit hexadecimal number, but note that the space is replaced by the + number for historical reasons. Rawurlencode in fact, like UrlEncode, is also used to encode strings, the only difference is that it uses RFC1738 encoding, which will replace the space with%20.

Their corresponding decoding functions are UrlDecode and Rawurldecode. Referring to the official website's description, UrlDecode decodes any%## in the encoded string given, plus (' + ') is decoded into a space character, and the Rawurldecode decodes the character string with a percent sign (%) followed by a two-bit hex. There are two differences, one is UrlDecode decoding is the percent (%) after any two characters will be decoded, for example,%MN will also decode, but will fail; Rawurldecode only the percent percent (%) after the two digits are hexadecimal (0-9a-f) characters will be decoded, Second, the UrlDecode will decode the + number into a space.

Let's look at two more functions in the official PHP introduction.

urlencode-encoded URL string

Report a bug description
String UrlEncode (String $str)
This function makes it easy to encode a string and use it for the request part of the URL, and it also facilitates the passing of a variable to the next page.

Report a bug parameter

Str
The string to encode.

Report a bug return value
Returns a string, in addition to-_, in this string. All non-alphanumeric characters are replaced with a percent sign (%) followed by a two-digit hexadecimal number, and a space is encoded as a plus (+). This encoding is the same as the WWW form POST data, and is encoded in the same way as the application/x-www-form-urlencoded media type. For historical reasons, this encoding differs from RFC1738 encoding (see Rawurlencode ()) in terms of encoding spaces as plus signs (+).

The code is as follows Copy Code

Example #1 UrlEncode () example

Echo ';
?>


Example #2 UrlEncode () and htmlentities () example

$query _string = ' foo= '. UrlEncode ($foo). ' &bar= '. UrlEncode ($bar);
Echo ';
?>

rawurlencode-encoding URLs according to RFC 1738

Report a bug description
String Rawurlencode (String $str)
The characters specified according to the» RFC 3986 encoding.

Report a bug parameter

Str
The URL to encode.


Report a bug return value
Returns a string, in addition to-_, in this string. All non-alphanumeric characters are replaced with a percent (%) followed by a two-digit hexadecimal number. This is the encoding described in» RFC 3986 to protect the literal characters from being interpreted as a special URL delimiter, while protecting the URL format from being confused by the transfer media (like some messaging systems) using character conversions.

Cases

The code is as follows Copy Code

Example #1 include a password in the FTP URL

Echo ' @ftp. Example.com/x.txt ' > ';
?>
The above routines will output:

By the description of the decoding function on the face, it can be inferred that using UrlEncode or Rawurlencode encoding can be decoded using UrlDecode, but if the original string contains spaces, Strings that are decoded with UrlEncode encoded using Rawurlencode are different from the original string.

http://www.bkjia.com/PHPjc/628888.html www.bkjia.com true http://www.bkjia.com/PHPjc/628888.html techarticle in PHP rawurlencode and Rawurlencode are the characters are encoded, the following I will introduce UrlEncode and rawurlencode the difference, there is a need to understand the friends can refer to. The use of UrlEncode ...

  • Related Article

    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.