Reversing the effect of `mysqli_real_escape_string`

后端 未结 2 779
野的像风
野的像风 2021-01-16 18:59

I am working on an application where user can create HTML templates and save them to the database.The templates consist of different components like text,image etc.When I tr

相关标签:
2条回答
  • 2021-01-16 19:17

    stripslashes() will remove slashes from any string, and is useful when outputting escaped data.

    I believe you need to use this:

    json_encode($IdLessContent, JSON_UNESCAPED_SLASHES);

    with the UNESCAPED_SLASHES part, your data should be returned correctly.

    0 讨论(0)
  • 2021-01-16 19:21

    The monkeymatrix answer is incorrect for recent versions of PHP (I tested it on 7.3). mysqli_real_escape_string() only escapes certain characters. Here is a function that will reverse it:

    function reverse_mysqli_real_escape_string($str) {
        return strtr($str, [
            '\0'   => "\x00",
            '\n'   => "\n",
            '\r'   => "\r",
            '\\\\' => "\\",
            "\'"   => "'",
            '\"'   => '"',
            '\Z' => "\x1a"
        ]);
     }
    

    In a properly-written application, there should rarely be a need for this function. You should be using PDO with parameter binding to handle any escaping automatically. I only created this function to handle some legacy code that was escaping everything on input and I needed a way to get back to the original data.

    0 讨论(0)
提交回复
热议问题