PHP implements escape and unescape function code sharing in Javascript. escapeunescape_PHP tutorial

Source: Internet
Author: User
PHP implements escape and unescape function code sharing in Javascript, and escapeunescape. PHP implements code sharing of the escape and unescape functions in Javascript. the escapeunescape class is quite useful. why? PHP transmits GBK characters in JSON format, such as Chinese characters, Japanese characters, and Korean characters. Unicode is the most compatible with PHP to implement escape and unescape function code sharing in Javascript. escapeunescape

This class is quite useful. what is the function? PHP uses JSON to transmit GBK characters, such as Chinese, Japanese, and Korean. Unicode is the most suitable ..

<? Phpclasscoding {// simulate functions of the javascript escape and UNESCAPE functions functionunescape scape ($ 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) {// minutes -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) {// multi-byte 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="&#".$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)=="&#")            {                //echo substr($v,2,-1)."";                $ar[$k]=iconv("UCS-2","UTF-8",pack("n",substr($v,2,-1)));            }        }        returnjoin("",$ar);    } ?>

This category is quite easy to use. what is the function? PHP uses JSON to transmit GBK characters, such as the Unicode of Chinese, Japanese, and Korean...

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.