14

I have a piece of data that is large and will probably contain quotes and double quotes.

I'm using mysql_real_escape_string() to store it safely.

When I pull it out to look at it, all the quotes are escaped. I tried using str_replace to just get rid of all the quotes, but that turns \r\n's into rn. :(

How can I reverse it so the data comes back out the way it went in?

1
  • Useful question. Commented Mar 23, 2018 at 6:08

3 Answers 3

10

Who says?

$keyword = "TEST'TEST";

$result1 = mysql_real_escape_string($keyword);
echo $result1 --> TEST\'TEST

$result2 = nl2br(stripslashes($result));
echo $result2 --> TEST'TEST
Sign up to request clarification or add additional context in comments.

1 Comment

After escaping string, your string can become "TEST \r\n \'TEST", in that case nl2br will not work. So I did something like this: stripslashes(str_replace('\r\n' , '<br/>' , nl2br( "TEST \r\n \'TEST")))
7

Do you use magic quotes?

Note: If magic_quotes_gpc is enabled, first apply stripslashes() to the data. Using this function [mysql_real_escape_string] on data which has already been escaped will escape the data twice.

Comments

2

If you actually want to reverse that because of how your data is formatted, this is a solution I came up with:

function mysql_unreal_escape_string($string) {
    $characters = array('x00', 'n', 'r', '\\', '\'', '"','x1a');
    $o_chars = array("\x00", "\n", "\r", "\\", "'", "\"", "\x1a");
    for ($i = 0; $i < strlen($string); $i++) {
        if (substr($string, $i, 1) == '\\') {
            foreach ($characters as $index => $char) {
                if ($i <= strlen($string) - strlen($char) && substr($string, $i + 1, strlen($char)) == $char) {
                    $string = substr_replace($string, $o_chars[$index], $i, strlen($char) + 1);
                    break;
                }
            }
        }
    }
    return $string;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.