How to remove backslash on json_encode() function?

33

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

For example:

<?php
$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


56

json_encode($response, JSON_UNESCAPED_SLASHES);

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


51

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:

http://php.net/manual/en/function.json-encode.php

List of JSON constants:

http://php.net/manual/en/json.constants.php

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


Most Viewed Questions: