Question about url Chinese encoding when Curl is used to capture remote content

Source: Internet
Author: User
For URL encoding in PHP, urlencode () or rawurlencode () can be used. The difference between the two is that the former encodes spaces into & amp; #39; + & amp; #39 ;, the latter encodes spaces into & amp; #39; % 20 & amp; #39;. However, it should be noted that only part of the URL should be encoded, otherwise... "/> <for URL encoding in script PHP, urlencode () or rawurlencode () can be used. The difference between the two is that the former encodes space into '+ ', the latter encodes spaces into '% 20', but it should be noted that only part of the URL should be encoded, otherwise the colon and backslash in the URL will be escaped. The following is a detailed explanation: 1 string urlencode (string str) returns a string -_. all other non-alphanumeric characters will be replaced with a semicolon (%) followed by two hexadecimal numbers, and spaces will be encoded as the plus sign (+ ). Example 1: differences between urlencode and rawurlencode functions $ str = 'boke'; 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 = Boke "$ url = 'http: // www.baidu.com/s? Wd = boke'; $ arr = explode ('=', $ url); $ url = $ arr [0]. '= '. rawurlencode ($ arr [1]); echo $ url; result: the function cn_urlencode ($ url) may be encoded using the following url) {$ pregstr = "/[\ x {4e00}-\ x {9fa5}] +/u"; // UTF-8 Chinese regular if (preg_match_all ($ pregstr, $ url, $ matchArray) {// returns the array foreach ($ matchArray [0] as $ key => $ val) {$ url = str_replace ($ val, urlencode ($ val), $ url); // replace translation with Chinese characters} if (strpos ($ url ,'')) {// if there is a space $ url = str_replace ('', '% 20', $ url) ;}} return $ url ;}

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.