PHP address URL encoding garbled problem

Source: Internet
Author: User
Tags alphanumeric characters
PHP address URL encoding garbled problem

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

PHP in UrlEncode Introduction:

UrlEncode refers to the URL of the Web page in the Chinese character encoding conversion method, the most common is Baidu, Google and other search engines in the input of the query, the generation of Encode Web 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.scutephp.com/Chinese. RAR---browser automatically converted to Http://www.scutephp.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.scutephp.com/Chinese. RAR---browser automatically converted to Http://www.scutephp.com/%E4%B8%AD%E6%96%87.rar

Examples of UrlEncode and Rawurlencode:

Code

GB2312 's Encode


%d6%d0%ce%c4-_.+


Chinese-_.

Echo Rawurlencode ("Chinese-_."). " \ n ";

%d6%d0%ce%c4-_.%


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 resolved, resulting in the partition of the 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.

Code

$url = ' http://www.phpernote.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:

Code

function parseURL ($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:password@www.huikaiche.com/Chinese/English. rar";

echo parseURL ($url);

Ftp://yongfu:password@www.huikaiche.com/%d6%d0%ce%c4/%d6%d0%ce%c4.rar

  • 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.