In php, rawurlencode and rawurlencode both encode characters. next I will introduce the differences between urlencode and rawurlencode. if you need to know, refer to urlencode for encoding strings, except all the characters in the original string "-_".
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 to replace spaces 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 decoded 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-encoded URL string Report a bug description
String urlencode (string $ str)
This function allows you to encode a string and use it in the request part of the URL. It also allows you to pass variables to the string to be encoded by the next page. Report a bug parameter str.
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 instance code is as follows:
- Example #1 urlencode () Example
-
- Echo ', urlencode ($ userinput),' "> ';
- ?>
-
- Example #2 examples of urlencode () and htmlentities ()
-
- $ Query_string = 'foo = '. urlencode ($ foo).' & bar = '. urlencode ($ bar );
- Echo '. htmlentities ($ query_string).' "> ';
- ?>
Rawurlencode-encode the URL according to RFC 1738. Report a bug description
String rawurlencode (string $ str)
The URL to be encoded by the str parameter of the character. Report a bug specified by RFC 3986.
Report a bug return value
Returns a string -_. all other non-alphanumeric characters 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.
The instance code is as follows:
- Example #1 include a password in the FTP URL
-
- Echo ', rawurlencode ('foo @ + % /'),
- '@ Ftp.example.com/x.txt "> ';
- ?>
The above routine will output:
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.