url的在線編碼和解碼工具:http://tool.chinaz.com/tools/urlencode.aspx
1、基本知識
URLEncode:是指針對網頁url中的中文字符的一種編碼轉化方式,最常見的就是Baidu、 Google等搜索引擎中輸入中文查詢時候,生成經過 Encode過的網頁URL。URLEncode的方式一般有兩種一種是傳統的基于GB2312的Encode(Baidu、Yisou等使用),一種是 基于UTF-8的Encode(Google,Yahoo等使用)。本工具分別實現兩種方式的Encode與Decode。
中文 -> GB2312的Encode -> ????
中文 -> UTF-8的Encode -> 中文
Html中的URLEncode:
編碼為GB2312的html文件中,
http://ud03.kinoko.name/中文.rar -> 瀏覽器自動轉換為 -> http://ud03.kinoko.name/????.rar
注意:Firefox對GB2312的Encode的中文URL支持不好,因為它默認是UTF-8編碼發(fā)送URL的,但是ftp://協議可以,我試過了.我認為這應該算是Firefox一個bug.
編碼為UTF-8的html文件中,
http://ud03.kinoko.name/中文.rar -> 瀏覽器自動轉換為 -> http://ud03.kinoko.name/中文.rar
PHP中的URLEncode:
//GB2312的Encode
echo urlencode(“中文-_. “).”\n”; // ????-_.+
echo urldecode(“????-_. “).”\n”; //中文-_.
echo rawurlencode(“中文-_. “).”\n”; // ????-_.
echo rawurldecode(“????-_. “).”\n”; //中文-_.
?>
除了 -_. 之外的所有非字母數字字符都將被替換成百分號(%)后跟兩位十六進制數。
urlencode和rawurlencode的區(qū)別:
urlencode 將空格則編碼為加號(+)
rawurlencode 將空格則編碼為加號( )
如果要使用UTF-8的Encode,有兩種方法:
一、將文件存為UTF-8文件,直接使用urlencode、rawurlencode即可。
二、使用mb_convert_encoding函數。
$url = ‘http://ud03.kinoko.name/中文.rar’;
echo urlencode(mb_convert_encoding($url, ‘utf-8′, ‘gb2312′)).”\n”;
echo rawurlencode(mb_convert_encoding($url, ‘utf-8′, ‘gb2312′)).”\n”;
//http://ud03.kinoko.name/中文.rar
?>
實例:
function parseurl($url=”")
{
$url = rawurlencode(mb_convert_encoding($url, ‘gb2312′, ‘utf-8′));
$a = array(“:”, “/”, “@″);
$b = array(“:”, “/”, “@”);
$url = str_replace($a, $b, $url);
return $url;
}
$url=”ftp://ud03:password@ud03.kinoko.name/中文/中文.rar”;
echo parseurl($url);
//ftp://ud03:password@ud03.kinoko.name/????/????.rar
?>
JavaScript中的URLEncode:
中文-_. 中文-_.
encodeURI 不對下列字符進行編碼:“:”、“/”、“;”、“?”、“@”等特殊字符。
http://ud03.kinoko.name/中文.rarhttp://ud03.kinoko.name/中文.rar
2、示例
http://canvas.gdt.qq.com/canvas/1?viewid=%12%0C%08%E1%98%B7%CD%CB%DC%14%20%E4%A5%01%18%FF%89%18&ckn=91142321196129
1)utf-8解碼
http://canvas.gdt.qq.com/canvas/1?viewid=?????? ??????&ckn=91142321196129
2)gb2312解碼
http://canvas.gdt.qq.com/canvas/1?viewid=?針吠塑? 湞???&ckn=91142321196129