PHP--------Address URL encoding issues

Source: Internet
Author: User
Tags rar urlencode alphanumeric characters

In PHP there are UrlEncode (), UrlDecode (), Rawurlencode (), Rawurldecode () functions to solve the problem of URL encoding and decoding of Web pages.

Understanding UrlEncode:

UrlEncode: Refers to the URL of the Web page in the Chinese characters of an encoding conversion, the most common is Baidu, Google and other search engines in the input of English Query time, generated through the Encode page URL.

UrlEncode generally have two kinds of traditional GB2312-based encode (Baidu, Yisou, etc.), one is based on utf-8 (encode, etc.). In this paper, the encode and decode of two kinds of methods are analyzed respectively.

Encode,%D6%D0%CE%C4, GB2312, Chinese
Encode,%e4%b8%ad%e6%96%87, Utf-8, Chinese

UrlEncode in HTML:

In an HTML file encoded as GB2312:
http://www.php.com/Chinese. RAR---browser automatically converted to Http://www.php.com/%D6%D0%CE%C4.rar
Note: Firefox is not good for the Chinese URL of GB2312 encode because it defaults to UTF-8 encoding to send the URL, but the ftp://protocol can be considered a bug in Firefox.

In an HTML file encoded as Utf-8:
http://www.php.com/Chinese. RAR---browser automatically converted to Http://www.php.com/%E4%B8%AD%E6%96%87.rar

UrlEncode in PHP:

// GB2312 's encode Echo UrlEncode // %d6%d0%ce%c4-_.+ Echo UrlDecode // Chinese-_. Echo Rawurlencode // %d6%d0%ce%c4-_.%20 Echo Rawurldecode // Chinese-_.

Except-_. All non-alphanumeric characters are replaced with a percent (%) followed by a two-digit hexadecimal number.

The difference between UrlEncode and Rawurlencode:

UrlEncode the space is encoded as a plus sign (+)
Rawurlencode the space is encoded as a plus sign (%20)

my last version of the TXT file splitter (online) code is the use of urlencode, never found this problem, resulting in a serious bug today, all the URLs with spaces can not be solved analysis, resulting in the partition of a good file can not be downloaded. This problem is solved by using the Rawurlencode () function.

If you want to use Utf-8 's encode, there are two ways:

One, save the file as Utf-8 file, directly use UrlEncode, Rawurlencode can.
Second, use the Mb_convert_encoding function.

$url = ' http://www.php.com/Chinese. rar '; Echo UrlEncode (Mb_convert_encoding ($url, ' utf-8 ', ' gb2312 ')). " \ n "; Echo Rawurlencode (Mb_convert_encoding ($url, ' utf-8 ', ' gb2312 ')). " \ n "; // Http%3a%2f%2fwww.huikaiche.com%2f%e4%b8%ad%e6%96%87.rar

Application Examples:

functionparseURL ($url=""){    $url=Rawurlencode(Mb_convert_encoding ($url, ' gb2312 ', ' utf-8 ')); $a=Array("%3a", "%2f", "%40"); $b=Array(":", "/", "@"); $url=Str_replace($a,$b,$url); return $url;}$url= "Ftp://yongfu:[email protected]/Chinese/English. rar";EchoparseURL ($url);//Ftp://yongfu:[email Protected]/%d6%d0%ce%c4/%d6%d0%ce%c4.rar

PHP--------Address URL encoding issues

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.