In PHP, the URL is encoded, you can use UrlEncode () or Rawurlencode (), the difference is that the former to encode the space as ' + ', and the latter to encode the space as '% 20 ', however, it should be noted that only part of the URL should be encoded at the time of encoding, otherwise the colon and backslash in the URL will also be escaped. Here is a detailed explanation:
Copy CodeThe code is as follows:
String UrlEncode (String str)
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 (+).
example 1:urlencode function differs from Rawurlencode function
Copy CodeThe code is as follows:
$str = ' blog ';
echo UrlEncode ($STR);
echo "
";
echo Rawurlencode ($STR);
URL Result:
Copy CodeThe code is as follows:
%b2%a9+%bf%cd
%b2%a9%20%bf%cd
Example 2:url Chinese encoding method
From URL: "http://www.baidu.com/s?wd= blog" to URL: "Http://www.baidu.com/s?wd=%E5%8D%9A%20%E5%AE%A2";
Copy CodeThe code is as follows:
$url = ' http://www.baidu.com/s?wd= blog ';
$arr =explode (' = ', $url);
$url = $arr [0]. ' = '. Rawurlencode ($arr [1]);
echo $url;
Results:
Http://www.baidu.com/s?wd=%E5%8D%9A%20%E5%AE%A2
Perhaps use the following URL encoding function
Copy CodeThe code is as follows:
function Cn_urlencode ($url) {
$pregstr = "/[\x{4e00}-\x{9fa5}]+/u";//utf-8 Chinese regular
if (Preg_match_all ($pregstr, $url, $matchArray)) {//Match Chinese, return array
foreach ($matchArray [0] as $key = + $val) {
$url =str_replace ($val, UrlEncode ($val), $url);//translate translation into Chinese
}
if (Strpos ($url, ')) {//If there are spaces
$url =str_replace (', '%20 ', $url);
}
}
return $url;
}
URL Result:
Http://www.baidu.com/s?wd=%E5%8D%9A%20%E5%AE%A2
http://www.bkjia.com/PHPjc/824958.html www.bkjia.com true http://www.bkjia.com/PHPjc/824958.html techarticle PHP in the code for the URL, you can use UrlEncode () or Rawurlencode (), the difference is that the former space code as ' + ', and the latter to encode the space as '%20 ', but should pay attention to ...