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

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

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

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

  1. PHP:
  2. urlencode( ) All punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding. Spaces converted to +.
  3.  
  4. urldecode( )All punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding. Spaces converted to +.
  5.  
  6. Javascript:
  7. escape(str) All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding

事例:

  1. $str = uniDecode($str,'big-5');
  2.  
  3.  
  4. function uniDecode($str,$charcode){
  5. $text = preg_replace_callback("/%u[0-9A-Za-z]{4}/",toUtf8,$str);
  6. return mb_convert_encoding($text, $charcode, 'utf-8');
  7. }
  8. function toUtf8($ar){
  9. foreach($ar as $val){
  10. $val = intval(substr($val,2),16);
  11. if($val < 0x7F){ // 0000-007F
  12. $c .= chr($val);
  13. }elseif($val < 0x800) { // 0080-0800
  14. $c .= chr(0xC0 | ($val / 64));
  15. $c .= chr(0x80 | ($val % 64));
  16. }else{ // 0800-FFFF
  17. $c .= chr(0xE0 | (($val / 64) / 64));
  18. $c .= chr(0x80 | (($val / 64) % 64));
  19. $c .= chr(0x80 | ($val % 64));
  20. }
  21. }
  22. return $c;
  23. }

No Comments »

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值