PHP character escaping function reference

Source: Internet
Author: User
Tags php regular expression
    1. if (Phpversion () < ' 5.3.0 ') {
    2. Set_magic_quotes_runtime (0);
    3. }
Copy Code

>> cannot define MAGIC_QUOTES_GPC through functions, so it is recommended to open the server uniformly, when writing the program should be judged, to avoid the security problems when the GPC is not turned on by Addslashes to the GPC when escaping, You should be aware of the filtering of key values and values when the user submits the array data

    1. if (!GET_MAGIC_QUOTES_GPC ()) {

    2. $_get = Daddslashes ($_get);
    3. $_post = Daddslashes ($_post);
    4. $_cookie = Daddslashes ($_cookie);
    5. $_files = Daddslashes ($_files);
    6. }
    7. function Daddslashes ($string, $force = 1) {
    8. if (Is_array ($string)) {
    9. foreach ($string as $key = = $val) {
    10. Unset ($string [$key]);
    11. $string [Addslashes ($key)] = Daddslashes ($val, $force);
    12. }
    13. } else {
    14. $string = Addslashes ($string);
    15. }
    16. return $string;
    17. }

    18. ?>>

Copy Code

Prevent XSS exploits by escaping HTML entities at user input or output!

Today, I came across a special character to handle the file, and again notice the problem in PHP:

    1. $str = "FFFF\0FFFF";
    2. Echo (strlen ($STR));
    3. Echo ("\ n");
    4. for ($i =0; $i
    5. Echo ("\ n");
Copy Code

Output Result:----------------------9 102 102 102 102 0 102 102 102 102

Examples of replacing special characters

    1. $str = "FFFF\0FFFF";
    2. $str = Str_replace ("\x0", "", $str);
    3. or with $STR = Str_replace ("n", "", $str);
    4. or with $STR = Str_replace (chr (0), "", $str);
    5. Echo (strlen ($STR));
    6. Echo ("\ n");
    7. for ($i =0; $i
    8. Echo ("\ n");
Copy Code

Output Result:----------------------8 102 102 102 102 102 102 102 102

An example of an octal ASCII code:

    1. Note that a string that conforms to the regular \[0-7]{1,3} represents an octal ASCII code.
    2. $str = "\0\01\02\3\7\10\011\08\8"; The \8 here do not meet the requirements and are revised to "\\8" (ASCII 92 and 56)
    3. Echo (strlen ($STR));
    4. Echo ("\ n");
    5. for ($i =0; $i
    6. Echo ("\ n");
Copy Code

Output:----------------------11 0 1 2 3 7 8 9 0 56 92 56

Example of hexadecimal ASCII code:

    1. $str = "\x0\x1\x2\x3\x7\x8\x9\x10\x11\xff";
    2. Echo (strlen ($STR));
    3. Echo ("\ n");
    4. for ($i =0; $i
    5. Echo ("\ n");
Copy Code

Output:----------------------0 1 2 3 7 8 9 255 PHP special character escapes details php regular expression escape character php common escape character function Php escaped regular expression characters PHP implementation anti-injection and form submission value Escaped code a method for escaping characters in a MySQL statement an example of PHP character escapes related considerations

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