I am generating JSON with PHP.
I have been using
$string = 'This string has "double quotes"';
echo addslashes($string);
outputs: This string has " double quotes"
Perfectly valid JSON
Unfortunately addslashes also escapes single quotes with catastrophic results for valid JSON
$string = "This string has 'single quotes'";
echo addslashes($string);
outputs: This string has 'single quotes'
In short, is there a way to only escape double quotes?
See Question&Answers more detail:os