phpjsonaddslashes

addslashes and json_encode not working together


$val = "I'm string";

For this type of string I am using the addslashes function, which convert string into like this:

"I\'m string"

and store into the database. When I get all data from database in array of fields and passed array in

json_encode($arr);

In response I get the string with a extra slash like this:

"I\\'m string"

And I wanted to remove that extra slash which is added by json_encode. how I do that??


Solution

  • If you really want an answer to this question you can reverse your addslashes with stripslashes.

    But never use addslashes function to escape values you are going to send to mysql.

    Use native prepared statements, mysqli_real_escape_string() or PDO::quote.

    BUT NOTE:

    1. Don't use a vulnerable character set for connection encoding (use utf8 or something)
    2. Use a higher version of MySQL than 5.7.6.

    Read more about character set issues here: http://php.net/manual/en/mysqlinfo.concepts.charset.php