Home > Backend Development > PHP Tutorial > URL Chinese encoding problem when using Curl to crawl remote content_PHP tutorial

URL Chinese encoding problem when using Curl to crawl remote content_PHP tutorial

WBOY
Release: 2016-07-15 13:21:58
Original
933 people have browsed it

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'. However, it should be noted that when encoding Only part of the URL should be encoded, otherwise colons and backslashes in the URL will also be escaped. Here is a detailed explanation:

1
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
$str='blog';
echo urlencode($str);
echo "
";
echo rawurlencode($str);
url result:
%B2%A9+%BF%CD
%B2%A9%20%BF%CD
Example 2: URL Chinese encoding method
From url: "http://www.baidu.com/s?wd=blog"
$url='http://www.baidu.com/s?wd=blog';
$arr=explode('=',$url);
$url=$arr[0].'='.rawurlencode($arr[1]);
echo $url;
Result:
Maybe use the following url encoding function
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);//Replace translation with Chinese
}
if(strpos($url,' ')){//If there are spaces
$url=str_replace(' ','%20',$url);
}
}
return $url;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477128.htmlTechArticleTo encode URLs in PHP, you can use urlencode() or rawurlencode(). The difference between the two is that the former Spaces are encoded as +, while the latter encodes spaces as %20, but it should be noted that...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template