Javascript unescape () escape () character encoding and decoding function

Source: Internet
Author: User
Tags ord pack php tutorial

The escape () function can encode a string so that the string can be read on all computers.

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

Return value
A copy of the encoded string. Some 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 )));
// $ Tmpstring = substr ($ tmpstring,). substr ($ tmpstring,); this option may be enabled in window
$ Retrunstring. = "% u". $ tmpstring;
$ I ++;
} Else {
$ Retrunstring. = "%". dechex (ord ($ str [$ I]);
                  }
         }
Return $ retrunstring;


The unescape () function can decode strings encoded by escape.

Syntax
Unescape (string) parameter description
String is required. The string to be decoded or reversed.

Return value
String is a decoded copy.

Description
This function works like this: find the character sequence in the form of % xx and % uxxxx (x indicates a hexadecimal number ), decodes character sequences such as unicode u00xx and uxxxx.

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) = "&#"){
$ Ar [$ k] = iconv ("UCS-2", "gbk", pack ("n", substr ($ v, 2,-1 )));
                  }
         }
Return join ("", $ ar );
}

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.