Home > Backend Development > PHP Tutorial > javascript unescape() escape() character encoding and decoding function_PHP tutorial

javascript unescape() escape() character encoding and decoding function_PHP tutorial

WBOY
Release: 2016-07-13 10:54:14
Original
830 people have browsed it

The

escape() function encodes a string so that it can be read on all computers.

Grammar
escape(string) parameter description
string required. The string to be escaped or encoded.

Return value
A copy of the encoded string. Some of the characters are replaced with hexadecimal escape sequences

function php tutorial escape($str)
{
            $sublen=strlen($str);
           $retrunstring="";
for ($i=0;$i<$sublen;$i++)
                                                                          If(ord($str[$i])>=127)
                                                                                      $ TMPS tutorial tring = bin2hex (iconv ("GB2312", "UCS-2", substr ($ STR, $ i, 2));
// $tstring=substr. $retrunstring.="%u".$tmpstring;
$i++;
                        } else {
$retrunstring.="%".dechex(ord($str[$i]));
                                                                                                                }            }
            return $retrunstring;
}



The unescape() function decodes a string encoded with escape().

Grammar

unescape(string) parameter description
string required. The string to decode or unescape.

Return value
A decoded copy of string.

Description

The function works by decoding by finding character sequences of the form %xx and %uxxxx (x represents a hexadecimal number) and replacing such character sequences with the unicode characters u00xx and uxxxx.

php processing


function unescape($str) {

            $str = rawurldecode($str);

Preg_match_all("/%u.{4}|.{4};|d+;|.+/u",$str,$r);
           $ar = $r[0];

foreach($ar as $k=>$v) {
If(substr($v,0,2) == "%u")
                           $ar[$k] = iconv("ucs-2","gbk",pack("h4",substr($v,-4)));
                    elseif(substr($v,0,3) == "")
                             $ar[$k] = iconv("ucs-2","gbk",pack("h4",substr($v,3,-1)));
                    elseif(substr($v,0,2) == "") {
                             $ar[$k] = iconv("ucs-2","gbk",pack("n",substr($v,2,-1)));
                                                                                                                                                                         }
           return join("",$ar);
}


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632345.htmlTechArticleescape() function encodes a string so that it can be read on all computers . Syntax escape(string) Parameter Description string Required. Words to be escaped or encoded...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template