- 論壇徽章:
- 0
|
在使用AJAX的時候,在URL中使用中文傳遞經(jīng)常會出現(xiàn)編碼錯誤的。今天本來用以前那個unescape()來進行解碼,后來發(fā)現(xiàn)服務器居然沒有打開iconv擴展,超級汗,不得不找了一個類似的函數(shù),主要從utf-8轉(zhuǎn)化成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;
}
這個函數(shù)轉(zhuǎn)化過來的是個html實體的串,不影響顯示但在數(shù)據(jù)庫沒有可讀性,所以使用$value=mb_convert_encoding($value,'GB2312','HTML-ENTITIES');來轉(zhuǎn)化成適合自己需要的編碼
另外附上iconv版的函數(shù):
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 |
|