Parsing the escape function in PHP _php tutorial

Source: Internet
Author: User
Use JS to the URL of the Chinese characters to escape coding.
When you click the link, the effect is:
Reference: http://127.0.0.1/shop/product_list.php?p_sort=PHP%u5F00%u53D1%u8D44%u6E90%u7F51
The result is that the UrlDecode () or Base64_decode () in PHP is clearly not solvable.
workaround, write an inverse function in PHP:
Copy CodeThe code is as follows:
function Js_unescape ($STR) {
$ret = ";
$len = strlen ($STR);
for ($i = 0; $i < $len; $i + +)
{
if ($str [$i] = = '% ' && $str [$i +1] = = ' U ')
{
$val = Hexdec (substr ($str, $i +2, 4));
if ($val < 0x7f) $ret. = Chr ($val);
else if ($val < 0x800) $ret. = Chr (0xc0| ( $val >>6). Chr (0x80| ( $val &0x3f)); else $ret. = Chr (0xe0| ( $val >>12). Chr (0x80| ( ($val >>6) &0x3f). Chr (0x80| ( $val &0x3f));
$i + = 5;
}
else if ($str [$i] = = '% ')
{
$ret. = UrlDecode (substr ($str, $i, 3));
$i + = 2;
}
else $ret. = $str [$i];
}
return $ret;}

Note that the JS encoding will automatically be converted into UTF-8, so the encoding must be converted to get the correct results, otherwise it will be garbled in Chinese. However, this step is not necessary if you use UTF-8 encoding.
The code is as follows: Print iconv (' utf-8 ', ' gb2312 ', Js_unescape ($_request[' p_sort '));
To this we have successfully reversed the JS escape code.
As follows:
In addition, I found a PHP implementation of the Escape encoding function:
Copy CodeThe code is as follows:
function Phpescape ($STR)
{
$sublen =strlen ($STR);
$retrunString = "";
for ($i =0; $i < $sublen; $i + +)
{
if (Ord ($str [$i]) >=127)
{
$tmpString =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;
}

In the JSON does not support Chinese, use it to send Chinese data will be lost or garbled data, must be sent before transmitting the string to be encoded, because the transmission in the past need to use JS for data parsing, considering JS has unescape function, so if there is an escape function in PHP, the data It is much easier to encode and decode the client with unescape.
First search on the internet, a lot of PHP implementation of the Escape function, similar, such as the following:
Copy CodeThe code is as follows:
function Phpescape ($STR) {
Preg_match_all ("/[\x80-\xff].| [\x01-\x7f]+/], $STR, $r);
$ar = $r [0];
foreach ($ar as $k = = $v) {
if (Ord ($v [0]) < 128)
$ar [$k] = Rawurlencode ($v);
Else
$ar [$k] = "%u". Bin2Hex (Iconv ("GB2312", "UCS-2", $v));
}
return join ("", $ar);
}

This function works well, but maybe a novice doesn't understand the principle of the function (like me), it's always reassuring to use it, and now I'm going to explain how this function works. And I think it's like standing on the shoulders of giants, but if you don't understand someone else's code, it's going to fall to the ground sooner or later.
The first sentence:Preg_match_all ("/[\x80-\xff].| [\x01-\x7f]+/, $str, $r); This is to match all the characters in the string with a regular expression, [\x80-\xff]. Matches the kanji, \x represents the 16 encoding of the matched character, [] is the class selector, and "." represents any one character, so [\x80-\xff]. Matches two characters, the first of which is 16 characters from 80 to FF, which is exactly the first character of Chinese character coding. This will completely match a Chinese character. For the encoding of Chinese characters in Unicode, you can search the Internet for a bit. Similarly, [\x01-\x7f]+ English string, because the earliest English is ASCII encoding, the encoded value is less than 128, that is, 16 binary from 01 to 7f, "+" means one or more characters, so [\x01-\x7f]+ can match a number of consecutive English strings.
Copy CodeThe code is as follows:
$ar = $r [0]; $r [0] where the storage is the matching array
foreach ($ar as $k = = $v) {
if (Ord ($v [0]) < 128)//If the character encoding value is less than 128, the description is an English character
$ar [$k] = Rawurlencode ($v); Direct encoding with Rawurlencode
Else
$ar [$k] = "%u". Bin2Hex (Iconv ("GB2312", "UCS-2", $v)); Otherwise, use the Iconv function to convert Chinese characters into ucs-2 encoding, which is Unicode encoding
}

In JavaScript, you can use unescape to decode the
\u0391-\uffe5 and \u4e00-\u9fa5 to match Chinese
But it seems that the former contains Chinese characters under the A-¥ and so the latter may be pure Chinese characters.
Where the decoding function is:
Copy CodeThe code is as follows:
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);
}

One, the coding range
1. GBK (gb2312/gb18030)
\x00-\xff GBK Double byte encoding range
\x20-\x7f ASCII
\xa1-\xff Chinese
\x80-\xff Chinese
2. UTF-8 (Unicode)
\U4E00-\U9FA5 (English)
\x3130-\x318f (Korean
\XAC00-\XD7A3 (Korean)
\u0800-\u4e00 (Japanese)
PS: Korean is more than [\U9FA5] characters
the regular example:
Preg_replace ("/([\x80-\xff])/", "", $str);
Preg_replace ("/([U4E00-U9FA5])/", "", $str);

http://www.bkjia.com/PHPjc/327931.html www.bkjia.com true http://www.bkjia.com/PHPjc/327931.html techarticle use JS to the URL of the Chinese characters to escape coding. A href= "onclick=" window.open (' product_list.php?p_sort= ' +escape (' home of the Script ')); When you click the link, the effect is: reference: http:/ ...

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