Chinaunix首页 | 论坛 | 博客
  • 博客访问: 434390
  • 博文数量: 50
  • 博客积分: 5071
  • 博客等级: 大校
  • 技术积分: 1780
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-20 10:36
文章分类

全部博文(50)

文章存档

2011年(2)

2010年(6)

2009年(12)

2008年(30)

我的朋友

分类:

2008-12-26 13:01:39

在使用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);
}

阅读(817) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-06-03 12:28:23

as