js 调php自定义函数,PHP对JS函数escape的解析自定义函数代码

将js的以%u开头的unicode编码(标准应为&#开头),转换为utf-8编码.

function unescape($source, $iconv_to = 'UTF-8') {

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

$decodedStr .= code2utf($unicode);

$pos += 4;

}

else {

// we have an escaped ascii character

$hexVal = substr ($source, $pos, 2);

$decodedStr .= chr (hexdec ($hexVal));

$pos += 2;

}

}

else {

$decodedStr .= $charAt;

$pos++;

}

}

if ($iconv_to != "UTF-8") {

$decodedStr = iconv("UTF-8", $iconv_to, $decodedStr);

}

return $decodedStr;

}

function code2utf($num){

if($num<128)return chr($num);

if($num<2048)return chr(($num>>6)+192).chr(($num&63)+128);

if($num<65536)return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);

if($num<2097152)return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128) .chr(($num&63)+128);

return '';

}

$str = "%3d";

echo unescape($str);

?>

posted on 2008-02-27 17:22 十三郎 阅读(565) 评论(0)  编辑 收藏 引用 所属分类: 语言

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值