我用PHP生成JSON.
我一直在用
$string = 'This string has "double quotes"';
echo addslashes($string);
Run Code Online (Sandbox Code Playgroud)
输出: This string has \" double quotes\"
完全有效的JSON
不幸的是,对于有效的JSON,addslashes也会逃避单引号,带来灾难性后果
$string = "This string has 'single quotes'";
echo addslashes($string);
Run Code Online (Sandbox Code Playgroud)
输出: This string has \'single quotes\'
简而言之,有没有办法只能逃避双引号?