JavaScript unescape () escape () character encoding decoding function _php Tutorial

Source: Internet
Author: User
The escape () function encodes the string so that it can be read on all computers.

Grammar
Escape (String) parameter description
string is required. The string to be escaped or encoded.

return value
A copy of the encoded string. Some of these characters are replaced with 16 binary 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)));
$tmpstring =substr ($tmpstring, 2,2). substr ($tmpstring, 0,2); window may open this
$retrunstring. = "%u". $tmpstring;
$i + +;
} else {
$retrunstring. = "%". Dechex (Ord ($str [$i]);
}
}
return $retrunstring;
}


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

Grammar
Unescape (String) parameter description
string is required. The string to decode or reverse.

return value
A copy of the string that was decoded.

Description
This function works by finding a sequence of characters in the form%xx and%uxxxx (x for hexadecimal digits), decoding with Unicode characters u00xx and uxxxx replacing such sequences of characters.

PHP processing

function Unescape ($STR) {
$str = Rawurldecode ($STR);
Preg_match_all ("/%u.{4}|& #x .{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) = = "& #x")
$ar [$k] = Iconv ("Ucs-2", "GBK", Pack ("H4", substr ($v, 3,-1)));
ElseIf (substr ($v, 0,2) = = "the") {
$ar [$k] = Iconv ("Ucs-2", "GBK", Pack ("n", substr ($v, 2,-1)));
}
}
return join ("", $ar);
}

http://www.bkjia.com/PHPjc/632345.html www.bkjia.com true http://www.bkjia.com/PHPjc/632345.html techarticle The escape () function encodes the string so that it can be read on all computers. The syntax Escape (string) parameter describes the string required. The word to be escaped or encoded ...

  • 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.