在PHP中为JSON输出删除引号

在PHP中为JSON输出删除引号

问题描述:

I have a string in PHP that contains both single and double quotes.

I am trying to use this string in some JSON output. To do this I need to escape double quotes, slashes, newlines etc.

I thought that addslashes() would do the job but it also escapes single quotes which causes the JSON to fail.

How can I escape all relevant special characters except for single quotes?

我在PHP中有一个包含单引号和双引号的字符串。 p>

我试图在一些JSON输出中使用此字符串。 要做到这一点,我需要转义双引号,斜杠,换行符等。 p>

我认为addslashes()可以完成这项工作,但它也会转义单引号,导致JSON失败。 / p>

除了单引号外,如何转义所有相关的特殊字符? strong> p> div>

json_encode() will handle all that for you.