Php escape Functions

Source: Internet
Author: User
The URL encoding function provided by php is byte-based, and cannot be used to encode data from the javascript function escape of ie. So here I share my php version's escapeunescape function functionescape ($ str) {preg_match_all ([x80-xff]. | [x01-x7f] +, $ str, $ r); $ ar $ r [0]; foreach ($

The URL encoding function provided by php is byte-based, and cannot be used to encode data from the javascript function escape of ie. Therefore, you can share your php version of escape/unescape function escape ($ str) {preg_match_all ("/[/x80-/xff]. | [/x01-/x7f] +/", $ str, $ r); $ ar = $ r [0]; foreach ($

URL encoding provided by phpFunctionIs byte-based.FunctionThe escape encoded data is powerless.
Therefore, I will share my php version of escape/unescape here.Function

Function escape ($ 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 );
}

Function unescape ($ str ){
$ Str = rawurldecode ($ str );
Preg_match_all ("/(? : % U. {4}) |. +/", $ str, $ r );
$ Ar = $ r [0];
Foreach ($ ar as $ k => $ v ){
If (substr ($ v, 0, 2) = "% u" & strlen ($ v) = 6)
$ Ar [$ k] = iconv ("UCS-2", "GB2312", pack ("H4", substr ($ v,-4 )));
}
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.