PHP implementation of the Escape and Unescape function code sharing in JavaScript, escapeunescape_php tutorial

Source: Internet
Author: User

PHP implements the escape and Unescape function code sharing in JavaScript, Escapeunescape


This class is pretty good. function, PHP to do JSON pass GBK characters, such as Chinese, Japanese, Korean god horse Unicode most suitable.

<?phpclasscoding{//mimic the function of the escape and unescape functions of JavaScript functionunescape ($str) {$text =preg_replace_callback ("/%u [0-9a-za-z]    {4}/", Array (& $this, ' ToUtf8 '), $STR);  Returnmb_convert_encoding ($text, "gb2312", "utf-8");      } FunctiontoUtf8 ($ar) {foreach ($aras $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;    } functionescape ($string, $encoding = ' gb2312 ') {$return = '; for ($x =0; $x
 
  
   1)      {//multibyte character $return. = '%u '. Strtoupper (Bin2Hex (mb_convert_encoding ($str, ' UCS-2 ', $encoding)));      }else{$return. = '% '. Strtoupper (Bin2Hex ($STR));  }} Return$return; } Functiongb2utf8 ($string, $encoding = ' utf-8 ', $from _encode= ' gb2312 ') {returnmb_convert_encoding ($string, $encoding,  $from _encode); }}?>
  
 

Another similar script found on Google code

<?php Functionphpescape ($str) {$sublen =strlen ($STR);        $retrunString = ""; for ($i =0; $i < $sublen; $i + +) {if (Ord ($str [$i]) >=127) {$tmpString =bin2hex (                Iconv ("GBK", "Ucs-2", substr ($str, $i, 2)));                $tmpString =substr ($tmpString, 2,2). substr ($tmpString, 0,2);                $retrunString. = "%u". $tmpString;            $i + +;            }else{$retrunString. = "%". Dechex (Ord ($str [$i]);    }} return$retrunstring; } functionescape ($str) {Preg_match_all ("/[\x80-\xff].|        [\x01-\x7f]+/], $STR, $r);        $ar = $r [0];            foreach ($aras $k=> $v) {if (Ord ($v [0]) <128) $ar [$k]=rawurlencode ($v);        else $ar [$k]= "%u". Bin2Hex (Iconv ("UTF-8", "UCS-2", $v));    } returnjoin ("", $ar);        } functionphpunescape ($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 = "the". $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;        } functionunescape ($str) {$str =rawurldecode ($STR); Preg_maTch_all ("/(?:%u.{4}) |& #x. {4};|&#\d+;|.+/u", $str, $r);        $ar = $r [0];        #print_r ($ar); foreach ($aras $k=> $v) {if (substr ($v, 0,2) = = "%u") $ar [$k]=iconv ("UCS-2", "UTF-8", Pack ("H4            ", substr ($v,-4)));            ElseIf (substr ($v, 0,3) = = "& #x") $ar [$k]=iconv ("UCS-2", "UTF-8", Pack ("H4", substr ($v, 3,-1)));                ElseIf (substr ($v, 0,2) = = "I") {//echo substr ($v, 2,-1). "";            $ar [$k]=iconv ("UCS-2", "UTF-8", Pack ("n", substr ($v, 2,-1)));    }} returnjoin ("", $ar); }?>

http://www.bkjia.com/PHPjc/955972.html www.bkjia.com true http://www.bkjia.com/PHPjc/955972.html techarticle PHP implements the escape and Unescape function code sharing in JavaScript, Escapeunescape This class is quite useful. function, PHP to do JSON transfer GBK characters, such as Chinese, Japanese, Korean god horse Unicode most ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.