您的位置:首页 > Web前端 > JavaScript

用php来解析javascript函数escape编码过的字符串

2006-07-21 12:56 781 查看

用php来解析javascript函数escape编码过的字符串

Filed under: PHP Programming, Javascript Programming — ranbo @ 3:46 pm

PHP和Javascript都要HTML encoding的函数,但有区别,如下:

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:

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

事例:

$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;

}

No Comments »

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: