-
- function unescape($str) {
- $str = rawurldecode($str);
- preg_match_all("/(?:%u.{4})|. /",$str,$r);
- $ar = $r[0];
- foreach($ar as $k=>$v) {
- if(substr($v,0,2) == '%u' && strlen($v) == 6)
- $ar[$k] = iconv("UCS-2","utf-8",pack("H4",substr($v,-4)));
- }
- return join("",$ar);
- }
-
复制代码
有点小问题,又换一个函数,好像功能要强大一些。
-
- function unescape($str) {
- $str = rawurldecode($str);
- preg_match_all("/%u.{4}|.{4};|d ;|d ?|. /U",$str,$r);
- $ar = $r[0];
- foreach($ar as $k=>$v) {
- if(substr($v,0,2) == "%u")
- $ar[$k] = iconv("UCS-2","utf-8",pack("H4",substr($v,-4)));
- elseif(substr($v,0,3) == "")
- $ar[$k] = iconv("UCS-2","utf-8",pack("H4",substr($v,3,-1)));
- elseif(substr($v,0,2) == "") {
- $ar[$k] = iconv("UCS-2","utf-8",pack("n",preg_replace("/[^d]/","",$v)));
- }
- }
- return join("",$ar);
- }
-
复制代码
用了一段时间,发现在本地可以使用,但是我们的线上环境不能够使用。
线上是*nux,本地是XP了,还有,就是PHP版本不一样了。
后来,又在手册里面发现有一个类似的函数
而且还支持utf8,个人觉得应该通用性更好吧。
-
-
- //php字符转码
- 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;
- }
-
复制代码
使用此函数成功解决问题。
|