Home >Backend Development >PHP Tutorial >A brief analysis of php's urlencode() URL encoding function_PHP tutorial

A brief analysis of php's urlencode() URL encoding function_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:25:14805browse

There are generally two methods of URLEncoding, one is the traditional Encode based on GB2312 (used by Baidu, Yisou, etc.), and the other is based on UTF-8 Encode (used by Google, Yahoo, etc.).

This tool implements two methods of Encode and Decode respectively:

Chinese-> Encode of GB2312 -> %D6%D0%CE%C4

Chinese- > UTF-8 Encode -> %E4%B8%AD%E6%96%87

URLEncode in Html:

In html file encoded as GB2312: http:// /s.jb51.net/中文.rar -> The browser automatically converts to -> http://s.jb51.net/%D6%D0%CE%C4.rar

Note: Firefox The Chinese URL support for GB2312 Encode is not good because it sends URLs in UTF-8 encoding by default, but the ftp:// protocol is OK. I tried it. I think this should be regarded as a bug in Firefox.

In the html file encoded as UTF-8: http://s.jb51.net/中文.rar -> The browser automatically converts to -> http://s.jb51.net/ %E4%B8%AD%E6%96%87.rar

URLEncode in PHP:

Copy code The code is as follows:

//Encode of GB2312
echo urlencode("中文-_. ")."n"; //%D6%D0%CE%C4-_. +
echo urldecode("%D6%D0%CE%C4-_. ")."n"; //Chinese-_.
echo rawurlencode("中文-_. ")."n"; //%D6%D0%CE%C4-_.%20
echo rawurldecode("%D6%D0%CE%C4-_. ")."n"; //Chinese-_.
? >


All non-alphanumeric characters except "-_." will be replaced with a percent sign "%" followed by two hexadecimal digits.

The difference between urlencode and rawurlencode: urlencode encodes spaces as a plus sign "+", and rawurlencode encodes spaces as a plus sign "%20".

If you want to use UTF-8 Encode, there are two methods:

1. Save the file as a UTF-8 file and use urlencode or rawurlencode directly.

2. Use the mb_convert_encoding function:
Copy the code The code is as follows:

$url = 'http://s.jb51.net/中文.rar';
echo urlencode(mb_convert_encoding($url, 'utf-8', 'gb2312'))."n";
echo rawurlencode(mb_convert_encoding($url, 'utf-8', 'gb2312'))."n";
//http%3A%2F%2Fs.jb51.net%2F%E4%B8%AD%E6 %96%87.rar
?>


Example:
Copy code The code is as follows:

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://ud03:password@s.jb51.net/中文/ Chinese.rar";
echo parseurl($url);
//ftp://ud03:password@s.jb51.net/%D6%D0%CE%C4/%D6%D0%CE% C4.rar
?>


URLEncode in JavaScript:

such as: %E4%B8%AD%E6%96%87-_.% 20%E4%B8%AD%E6%96%87-_.%20

encodeURI does not encode the following characters: ":", "/", ";", "?", "@" and other special characters.

For example: http://s.jb51.net/%E4%B8%AD%E6%96%87.rarhttp%3A%2F%2Fs.jb51.net%2F%E4%B8%AD %E6%96%87.rar

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324192.htmlTechArticleThere are generally two methods of URLEncode, one is the traditional Encode based on GB2312 (used by Baidu, Yisou, etc.) , the other is Encode based on UTF-8 (used by Google, Yahoo, etc.). This tool is divided into...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn