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?
I just figured out that
json_encode
does only escape\n
if it's used within single quotes.And
Simpler way would be
If you using PHP 5.2, json_encode just expect only 1 parameter when call it. This is an alternative to unescape slash of json values:
Don't use it if your data is complicated.
Yes it's possible. Look!
But why would you want to?
You do not want to delete it. Because JSON uses double quotes " " for strings, and your one returns
these backslashes escape these quotes