PHP urlencode () function URL encoding conversion instance parsing

Source: Internet
Author: User
Tags rar urlencode alphanumeric characters

UrlEncode: is a Web page URL contains Chinese characters encoding conversion method, UrlEncode there are two common ways, one is based on GB2312 Encode (Baidu, Yisou and other search engines use), The other is based on the UTF-8 encode (Google, Yahoo, etc.). Here's a look at the difference between encode and decode in two ways:

Encode,%D6%D0%CE%C4, GB2312, Chinese

Encode,%e4%b8%ad%e6%96%87, UTF-8, Chinese

UrlEncode in HTML pages:

HTML file encoded as GB2312: http://www.baidu.com/. RAR---browser automatically converted to Http://www.baidu.com/%D6%D0%CE%C4.rar

Later found that Firefox to GB2312 encode Chinese URL support is not good, because it is the default UTF-8 encoding to send URL, but ftp://protocol can, this should be considered a bug in Firefox.

Let's look at a code example of UrlEncode encoding conversion in PHP:

// examples of encode in GB2312 Echo UrlEncode // %d6%d0%ce%c4-_.+ Echo UrlDecode // Chinese-_. Echo Rawurlencode // %d6%d0%ce%c4-_.%20 Echo Rawurldecode // Chinese-_.

Except for "-_." All non-alphanumeric characters will be replaced with a percent percent "%" followed by a two-digit hexadecimal number.

The difference between UrlEncode and Rawurlencode: UrlEncode encodes a space as a plus "+", and Rawurlencode encodes a space as a plus "%20".

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

The first is to save the file as a UTF-8 file, directly using UrlEncode, Rawurlencode.

The second method is to use the Mb_convert_encoding function transformation, the sample code is as follows:

$url= ' http://www.baidu.com/Chinese. rar '; Echo UrlEncode (Mb_convert_encoding ($url, ' utf-8 ', ' gb2312 ')). " \ n "; Echo Rawurlencode (Mb_convert_encoding ($url, ' utf-8 ', ' gb2312 ')). " \ n ";

Note: encodeURI does not encode special characters such as ":", "/", ";", "?", "@".

PHP urlencode () function URL encoding conversion instance parsing

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.