php的urlencode()URL编码函数浅析
URLEncode:是指针对网页url中的中文字符的一种编码转化方式,最常见的就是Baidu、Google等搜索引擎中输入中文查询时候,生成经过Encode过的网页URL。
echo urldecode("%D6%D0%CE%C4-_. ")."
"; //中文-_.
echo rawurlencode("中文-_. ")." "; //%D6%D0%CE%C4-_.%20
echo rawurldecode("%D6%D0%CE%C4-_. ")." "; //中文-_.
?>
echo rawurlencode("中文-_. ")." "; //%D6%D0%CE%C4-_.%20
echo rawurldecode("%D6%D0%CE%C4-_. ")." "; //中文-_.
?>
除了“-_.”之外的所有非字母数字字符都将被替换成百分号“%”后跟两位十六进制数。
urlencode和rawurlencode的区别:urlencode将空格编码为加号“+”,rawurlencode将空格编码为加号“%20”。
如果要使用UTF-8的Encode,有两种方法:
一、将文件存为UTF-8文件,直接使用urlencode、rawurlencode即可。
二、使用mb_convert_encoding函数:
echo rawurlencode(mb_convert_encoding($url, 'utf-8', 'gb2312'))."
";
//http%3A%2F%2Fs.jb51.net%2F%E4%B8%AD%E6%96%87.rar
?>
//http%3A%2F%2Fs.jb51.net%2F%E4%B8%AD%E6%96%87.rar
?>
实例:
echo parseurl($url);
//ftp://ud03:password@s.jb51.net/%D6%D0%CE%C4/%D6%D0%CE%C4.rar
?>
//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不对下列字符进行编码:“:”、“/”、“;”、“?”、“@”等特殊字符。
如:http://s.jb51.net/%E4%B8%AD%E6%96%87.rarhttp%3A%2F%2Fs.jb51.net%2F%E4%B8%AD%E6%96%87.rar