使用 PHP 解析 javascript escape() 编码过的字串

2005年05月05日 00:10 查看7638次 作者: Neo  【
文章分类:PHP技术文档


Neo 发表于 April 29, 200403:17 PM


PHP
Javascript 都有 HTML encoding 的函式,编码过的结果虽然类似,但是还是有些微的不同,所以不能互转。

二者的差异可以可参考:
http://php.weblogs.com/php_jscript_vbscript_1

节录如下:

PHP:
urlencode( ) All punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding. Spaces converted to +.

urldecode( )All punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding. Spaces converted to +.

Javascript:
Javascript(str) All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding

那要如何用 PHP 来读取 escape() 编码过的字符串呢?

可以用以下的方式来读取 (范例为 Unicode 解译为 big5)

修改自:http://vivian.stripper.jp/index.php?itemid=100

$str = uniDecode($str,'big-5');
 

function uniDecode($str,$charcode){

  $text = preg_replace_callback("/%u[0-9A-Za-z]{4}/",toUtf8,$str);

  return mb_convert_encoding($text, $charcode, 'utf-8');

}

function toUtf8($ar){

  foreach($ar as $val){

    $val = intval(substr($val,2),16);

    if($val < 0x7F){        // 0000-007F

        $c .= chr($val);

    }elseif($val < 0x800) { // 0080-0800

        $c .= chr(0xC0 | ($val / 64));

        $c .= chr(0x80 | ($val % 64));

    }else{                // 0800-FFFF

        $c .= chr(0xE0 | (($val / 64) / 64));

        $c .= chr(0x80 | (($val / 64) % 64));

        $c .= chr(0x80 | ($val % 64));

    }

  }

  return $c;

}

 

责任编辑:easy

给文章打个分先...

平均分:0.5(41 次)

-5 -4 -3 -2 -1 0 1 2 3 4 5
3

顶一下

发表我的见解...

  • 您的大名: 留空为匿名
  • 您的主页:
  • 您的邮箱:

看看大家怎么说...

  • 创意产品 发表于09年04月19日 19:40

    没怎么看懂。