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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…