Home>Article>Backend Development> [PHP] Briefly describe the differences between urlencode and rawurlencode
This article mainly talks about the differences between urlencode and rawurlencode in PHP. It has certain reference value and interested friends can learn about it.
Event background:
I heard from Daniel that before making h5 mini-games, the back-end needed to output user-related information to the front-end. The output content included: user id , user nickname and other fields, use the get method to pass parameters. The backend uses PHP language to format and encode the Chinese nickname, using the commonly used urlencode function.
A problem occurs:
Use the urlencode function for formatting. The urlencode function will encode spaces as:
Of course , the front end can decode and replace it with spaces when receiving. But this is an extra step, which is very troublesome. Sometimes our data interface is developed for third parties or others, and they may not necessarily follow this idea in processing.
For example, if the front end uses Python to receive it and directly decodes it and displays it, the nickname will have a number.
Solution:
Userawurlencode
In PHP, urlencode encodes spaces as No., rawurlencode encodes spaces as
PHP ManualExplanation of the two functions of:
##urlencode :Returns a string, returns a string, all non-alphanumeric characters in this string except -_. will be replaced with a percent sign (%) followed by two hexadecimal digits, and spaces Encoded as a plus sign ( ). This encoding is the same as the encoding of WWW form POST data, and the same as the media type encoding of application/x-www-form-urlencoded.For historical reasons, this encoding differs from the RFC1738 encoding (see rawurlencode()) in encoding spaces as plus signs ( ).
rawurlencode:Returns a string in which all non-alphanumeric characters except -_. will be replaced with a percent sign (%) followed by two hexadecimal digits. This encoding, described in RFC 1738, is intended to protect literal characters from being interpreted as special URL delimiters, and to protect the URL format from being garbled by character conversions used by the transport medium (like some mail systems).
So, when using PHP to transfer Chinese encoding, use rawurlencode
## Related courses:The above is the detailed content of [PHP] Briefly describe the differences between urlencode and rawurlencode. For more information, please follow other related articles on the PHP Chinese website!