PHP URL Chinese encoding cn_urlencode function _php Tutorial

Source: Internet
Author: User
Tags alphanumeric characters
PHP in the code for the URL, you can use UrlEncode () or Rawurlencode (), the difference is that the former space is encoded as \ ' +\ ', and the latter to encode the space as \ '%20\ ', but it should be noted that the encoding should be only part of the URL encoding, Otherwise, the colon and backslash in the URL are also escaped. Here is a detailed explanation:

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

The code is as follows Copy Code
$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" to Url:http://www.baidu.com/s?wd=%e5%8d%9a%20%e5%ae%a2;

The code is as follows Copy Code

$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

The code is as follows Copy Code
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;
}
echo Cn_urlencode ($url);
?>

URL Result:

Http://www.baidu.com/s?wd=%E5%8D%9A%20%E5%AE%A2

http://www.bkjia.com/PHPjc/629031.html www.bkjia.com true http://www.bkjia.com/PHPjc/629031.html techarticle PHP in the code for the URL, 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\ ', but should pay attention to ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.