How to remove backslash on json_encode() function?


How to remove the (\)backslash on a string? when using echo json_encode() ?

For example:

$str = "$(\"#output\").append(\"<p>This is a test!</p>\")";

echo json_encode($str);

note: When you echo $str, there will be no problem... but when you echo out using json_encode(), the (\)backslash will show up.

Is there a way to solve this?

This question is tagged with php ajax json function backslash

~ Asked on 2011-09-02 11:28:55

The Best Answer is


json_encode($response, JSON_UNESCAPED_SLASHES);

~ Answered on 2011-09-02 11:31:13


Since PHP 5.4 there are constants which can be used by json_encode() to format the json reponse how you want.

To remove backslashes use: JSON_UNESCAPED_SLASHES. Like so:

json_encode($response, JSON_UNESCAPED_SLASHES);

View the PHP documentation for more constants and further information:

List of JSON constants:

~ Answered on 2014-03-26 00:05:07

Most Viewed Questions: