Parsing escape function _ php Tutorial-PHP Tutorial

Source: Internet
Author: User
Parse the escape function in php. Use js to encode the Chinese characters in the URL. Ahrefonclickwindow. open (product_list.php? P_sort + escape (); when the link is clicked, reference: http: Use js to encode the Chinese characters in the URL.
The following figure shows the validity of the link:
Reference: http: // 127.0.0.1/shop/product_list.php? P_sort = PHP % u5F00 % u53D1 % u8D44 % u6E90 % u7F51
It is obvious that the urldecode () or base64_decode () of PHP cannot be reversed.
Solution: Use PHP to write an anti-solution function:

The 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 JS encoding will be automatically converted to UTF-8, so the encoding must be converted to get the correct results, otherwise the Chinese garbled. But this step is not needed if UTF-8 encoding is used.
The code is as follows: print iconv ('utf-8', 'gb2312', js_unescape ($ _ REQUEST ['p _ sort ']);
At this point, we have successfully reversed the js escape code.
As follows:
In addition, I found a function that uses PHP to implement js escape encoding:

The 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,). substr ($ tmpString,); this option may be enabled in window
$ RetrunString. = "% u". $ tmpString;
$ I ++;
} Else
{
$ RetrunString. = "%". dechex (ord ($ str [$ I]);
}
}
Return $ retrunString;
}


Json does not support Chinese characters. if you use json to transmit Chinese data, data may be lost or garbled. you must encode the string to be sent before sending the data, in the past, JavaScript was used for data parsing. considering that JavaScript contains the unescape function, if php has an escape function that encodes the data and decodes it on the client, this will be much more convenient.
First, search for one on the internet. many escape functions implemented using php are similar. for example:

The 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 can work very well, but some new users may not understand the principle of this function (such as me) and are always uneasy to use it. now I will explain the principle of this function. In addition, I think that using other people's code for reuse is like standing on the shoulders of giants, but if you don't understand other people's code, it will sooner or later fall to the ground.
First sentence:Preg_match_all ("/[\ x80-\ xff]. | [\ x01-\ x7f] +/", $ str, $ r); this is to use a regular expression to match all characters in the string, [\ x80-\ xff]. matching Chinese characters, \ x indicates the hexadecimal encoding of matching characters, and [] indicates the class selector, ". "represents any character, as shown in [\ x80-\ xff]. the match is two characters, the first of which is the hexadecimal character from 80 to ff, and this is exactly the first character of Chinese character encoding. In this way, a Chinese character can be completely matched. You can search for Chinese character encoding in unicode on the Internet. Similarly, [\ x01-\ x7f] + English string, because the earliest English is ASCII encoding, the encoding value is less than 128, that is, the hexadecimal value from 01 to 7f, "+" indicates one or more characters, so that [\ x01-\ x7f] + can match multiple consecutive English character strings.

The code is as follows:


$ Ar = $ r [0]; // $ r [0] stores matched arrays.
Foreach ($ ar as $ k => $ v ){
If (ord ($ v [0]) <128) // if the character encoding value is less than 128, it is an English character.
$ Ar [$ k] = rawurlencode ($ v); // use rawurlencode for encoding.
Else
$ Ar [$ k] = "% u ". bin2hex (iconv ("GB2312", "UCS-2", $ v); // otherwise, use the iconv function to convert Chinese characters into UCS-2 encoding, that is, unicode encoding.
}


In javascript, you can use unescape to decode it.
\ U0391-\ uFFE5 and \ u4e00-\ u9fa5 match Chinese characters
However, it seems that the former contains A-¥ and other Chinese characters, and the latter may be pure Chinese characters.
The decoding function is:

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


I. encoding range
1. GBK (GB2312/GB18030)
\ X00-\ xff GBK dubyte encoding range
\ X20-\ x7f ASCII
\ Xa1-\ xff Chinese
\ X80-\ xff Chinese
2. UTF-8 (Unicode)
\ U4e00-\ u9fa5 (Chinese)
\ X3130-\ x318F (Korean
\ XAC00-\ xD7A3 (Korean)
\ U0800-\ u4e00 (Japanese)
Ps: Korean is a character greater than [\ u9fa5]
Regular expression example:
Preg_replace ("/([\ x80-\ xff])/", "", $ str );
Preg_replace ("/([u4e00-u9fa5])/", "", $ str );

Bytes. A href = "" onclick = "window. open ('product _ list. php? P_sort = '+ escape ('feet home'); "when the link is clicked, 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.