Home >Backend Development >PHP Tutorial >A brief analysis of the difference between rawurlencode() and urlencode() functions_PHP tutorial
This article will introduce to you the difference and usage of rawurlencode() and urlencode() functions. Friends can refer to them.
Today when I was reading the open source code, I discovered a URL encoding function: rawurlencode(). I had never noticed it before because I mainly used urlencode() in daily development, so I studied it specially.
In fact, the difference between these two functions is very simple. Both of them will escape the non-English characters in the URL and replace them with "%" followed by two hexadecimal numbers. The difference is: rawurlencode complies with 94 According to the International Standard Memorandum RFC 1738, the escape of spaces is '%20'; and the encoding of urlencode implements the traditional method, which will escape spaces into "+" signs just like POST form data.
For example:
If the URL address source is in the form: http://www.bKjia.c0m encoding difference
rawurlencode($url):
http%3A%2F%2Fityizhan.com%2FPHP%20%E7%BC%96%E7%A0%81%E5%8C%BA%E5%88%AB%2B
urlencode($url):
http%3A%2F%2Fityizhan.com%2FPHP+%E7%BC%96%E7%A0%81%E5%8C%BA%E5%88%AB%2B
The difference between the two can be clearly seen from this example. If we want to decode the encoded URL, we can use these two functions: rawurldecode(), urldecode(). In order to avoid inconsistent URL formats in actual development, it is recommended that you use rawurlencode() to encode the URL.
The same operation also has some differences in Javascript. You can refer to: JavaScript URL encoding and decoding, which describes the encoding usage of escape(), encodeURI(), encodeURIComponent() in Javascript