欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php的urlencode()URL編碼函數(shù)淺析

 更新時間:2011年08月09日 12:03:10   作者:  
URLEncode:是指針對網(wǎng)頁url中的中文字符的一種編碼轉(zhuǎn)化方式,最常見的就是Baidu、Google等搜索引擎中輸入中文查詢時候,生成經(jīng)過Encode過的網(wǎng)頁URL。
URLEncode的方式一般有兩種,一種是傳統(tǒng)的基于GB2312的Encode(Baidu、Yisou等使用),另一種是基于UTF-8的Encode(Google、Yahoo等使用)。

本工具分別實(shí)現(xiàn)兩種方式的Encode與Decode:

中文 -> GB2312的Encode -> %D6%D0%CE%C4

中文 -> UTF-8的Encode -> %E4%B8%AD%E6%96%87

Html中的URLEncode:

編碼為GB2312的html文件中:http://s.jb51.net/中文.rar -> 瀏覽器自動轉(zhuǎn)換為 -> http://s.jb51.net/%D6%D0%CE%C4.rar

注意:Firefox對GB2312的Encode的中文URL支持不好,因?yàn)樗J(rèn)是UTF-8編碼發(fā)送URL的,但是ftp://協(xié)議可以,我試過了,我認(rèn)為這應(yīng)該算是Firefox一個bug。

編碼為UTF-8的html文件中:http://s.jb51.net/中文.rar -> 瀏覽器自動轉(zhuǎn)換為 -> http://s.jb51.net/%E4%B8%AD%E6%96%87.rar

PHP中的URLEncode:
復(fù)制代碼 代碼如下:

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


除了“-_.”之外的所有非字母數(shù)字字符都將被替換成百分號“%”后跟兩位十六進(jìn)制數(shù)。

urlencode和rawurlencode的區(qū)別:urlencode將空格編碼為加號“+”,rawurlencode將空格編碼為加號“%20”。

如果要使用UTF-8的Encode,有兩種方法:

一、將文件存為UTF-8文件,直接使用urlencode、rawurlencode即可。

二、使用mb_convert_encoding函數(shù):
復(fù)制代碼 代碼如下:

<?php
$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
?>


實(shí)例:
復(fù)制代碼 代碼如下:

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


JavaScript中的URLEncode:

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

encodeURI不對下列字符進(jìn)行編碼:“:”、“/”、“;”、“?”、“@”等特殊字符。

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

相關(guān)文章

最新評論