Implementation of Javascript escape () function code in PHP

Source: Internet
Author: User

Generally, normal strings must be encoded into the format that can be interpreted by the JavaScript unescape () function. For example, you can use the following function to implement escape () in Javascript () functions are the same:
Copy codeThe Code is as follows:
<? Php
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) {// multi-byte characters
$ 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:
Copy codeThe Code is as follows:
<? Php
// Please include the escapeToHex () function definition by yourself
$ Test = document. write (\ <a href = "http://www.dirk.sh/assets/uploaded/thisistest.pdf"> test </a> \);
Echo <script Language = "Javascript"> eval_r (unescape (". escapeToHex ($ test).") </script>;
?>

If you view the page source code, you will see (because it is too long, so the manual branch is carried out, the actual running result should be a complete line ):
Copy codeThe Code is as follows:
<Script Language = "Javascript"> eval_r (unescape ("% 64% 6F % 63% 75% 6D % 65% 6E % 74% 2E % 77 \
% 72% 69% 74% 65% 28% 3C % 27% 61% 20% 68% 72% 65% 3D % 66% 22% 68% 74% 74% 3A % 2F % 70% 77 \
% 2E % 64% 69% 72% 6B % 79% 2E % 6E % 65% 2F % 65% 74% 6C % 6F % 75% 70% 61% 2F % 64% 65% 64% 73 \
% 69% 73% 74% 65% 73% 74% 2E % 70% 64% 66% 22% 3E % 74% 65% 73% 74% 3C % 2F % 61% 3E % 27% 29 ") </script>

The page displayed in the browser is not different from the ordinary html.
Note:
1. The second parameter ($ encoding) of the escapeToHex () function represents the encoding of the string you passed in. The default is the UTF-8. If you use another encoding, it should be explicitly specified when the function is called;
2. unescape () is opposed to use in the ECMAScript v3 specification. We recommend that you use the new alternative function decodeURIComponent (). However, after testing, I found that decodeURIComponent () the function still uses the unescape () function because of a problem in processing multi-byte characters (Chinese characters.
3. In principle, the above method is only to prevent search crawlers from obtaining the resource addresses that you think need to be kept confidential and browsing the page in a browser that supports Javascript, the rendering is exactly the same if this protection mechanism is not enabled.

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.