Implementing JavaScript's Escape () function code in PHP _php tutorial

Source: Internet
Author: User
Here, it is generally necessary to pre-encode the normal string into a format that the JavaScript unescape () function can interpret, and in PHP, for example, you can use the following function to implement the same functionality as the Escape () function in javascript:
Copy CodeThe code is as follows:
function Escapetohex ($string, $encoding = UTF-8) {
$return =;
for ($x = 0; $x < Mb_strlen ($string, $encoding); $x + +) {
$str = Mb_substr ($string, $x, 1, $encoding);
if (strlen ($STR) > 1) {//multibyte character
$return. =%u. Strtoupper (Bin2Hex (mb_convert_encoding ($str, UCS-2, $encoding)));
} else {
$return. =%. Strtoupper (Bin2Hex ($STR));
}
}
return $return;
}
?>

Suppose we want to hide the following address: Http://www.dirk.sh/assets/uploaded/thisistest.pdf
We can use the following script to implement:
Copy CodeThe code is as follows:
Please include the Escapetohex () function definition yourself
$test = document.write (\test\);
echo;
?>

If you look at the source code of the page, you will see (because it is too long, so you have a manual branch, the actual result should be a complete line):
Copy CodeThe code is as follows:


The page displayed in the browser is not the same as the normal HTML.
Note:
1, the second parameter of the Escapetohex () function ($encoding) represents the encoding of the string that you pass in, the default is UTF-8, if you use other encodings, you should explicitly specify in the calling function;
2, Unescape () is objected to in the ECMAScript V3 specification, the specification recommends using the new alternative function decodeuricomponent (), but I have been tested to find that decodeuricomponent () The function has problems with multibyte character processing, so the unescape () function is still used.
3, in principle, the above method is only to prevent search crawlers to get the resource address you think you need to keep confidential, browse the page under Javascript-enabled browser, and see the rendering is identical without enabling this protection mechanism.

http://www.bkjia.com/PHPjc/322351.html www.bkjia.com true http://www.bkjia.com/PHPjc/322351.html techarticle here, it is generally necessary to pre-encode the normal string into a JavaScript unescape () function can be interpreted by the format, in PHP, for example, you can use the following functions to implement JavaScript ESCAP ...

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