- 论坛徽章:
- 0
|
在使用AJAX的时候,在URL中使用中文传递经常会出现编码错误的。今天本来用以前那个unescape()来进行解码,后来发现服务器居然没有打开iconv扩展,超级汗,不得不找了一个类似的函数,主要从utf-8转化成gb2312。
?
function utf8RawUrlDecode ($source) {
$decodedStr = "";
$pos = 0;
$len = strlen ($source);
while ($pos $len) {
$charAt = substr ($source, $pos, 1);
if ($charAt == '%') {
$pos++;
$charAt = substr ($source, $pos, 1);
if ($charAt == 'u') {
// we got a unicode character
$pos++;
$unicodeHexVal = substr ($source, $pos, 4);
$unicode = hexdec ($unicodeHexVal);
$entity = "&#". $unicode . ';';
$decodedStr .= utf8_encode ($entity);
$pos += 4;
}
else {
// we have an escaped ascii character
$hexVal = substr ($source, $pos, 2);
$decodedStr .= chr (hexdec ($hexVal));
$pos += 2;
}
} else {
$decodedStr .= $charAt;
$pos++;
}
}
return $decodedStr;
}
这个函数转化过来的是个html实体的串,不影响显示但在数据库没有可读性,所以使用$value=mb_convert_encoding($value,'GB2312','HTML-ENTITIES');来转化成适合自己需要的编码
另外附上iconv版的函数:
function unescape($str) {
$str = urldecode($str);
preg_match_all("/(?:%u.{4}|&#x.;|&#d+;|.+)/U",$str,$r);
$ar = $r[0];
foreach($ar as $k=>$v) {
if(substr($v,0,2) == "%u")
$ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,-4)));
elseif(substr($v,0,3) == "&#x")
$ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,3,-1)));
elseif(substr($v,0,2) == "&#") {
$ar[$k] = iconv("UCS-2","GB2312",pack("n",substr($v,2,-1)));
}
}
return join("",$ar);
}
function escape($str) {
preg_match_all("/[x80-xff].|[x01-x7f]+/",$str,$r);
$ar = $r[0];
foreach($ar as $k=>$v) {
if(ord($v[0]) 128)
$ar[$k] = rawurlencode($v);
else
$ar[$k] = "%u".bin2hex(iconv("GB2312","UCS-2",$v));
}
return join("",$ar);
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/13284/showart_1743838.html |
|