Home > Article > Backend Development > PHP url Chinese encoding cn_urlencode function_PHP tutorial
To encode URLs in PHP, you can use urlencode() or rawurlencode(). The difference between the two is that the former encodes spaces as '+', while the latter encodes spaces as '%20', but it should be noted that , you should only encode part of the URL when encoding, otherwise colons and backslashes in the URL will also be escaped. The following is a detailed explanation:
string urlencode ( string str)
Returns a string in which all non-alphanumeric characters except -_. will be replaced with a percent sign (%) followed by two hexadecimal digits, and spaces are encoded as plus signs ( +).
Example 1: The difference between urlencode function and rawurlencode function
The code is as follows
|
Copy code
|
||||||||||||
$str='blog'; echo urlencode($str); echo " "; echo rawurlencode($str);
url result: %B2%A9+%BF%CD
From url: "http://www.baidu.com/s?wd=blog" to url: http://www.baidu.com/s?wd=%E5%8D%9A%20%E5%AE %A2;
|