如何删除 json_encode() 函数上的反斜杠?
如何删除字符串上的(\)
反斜杠?当使用 echo json_encode() 时?
例如:
<?php
$str = "$(\"#output\").append(\"<p>This is a test!</p>\")";
echo json_encode($str);
?>
注意:当你 echo $str 时,不会有问题...但是当你使用 json_encode()
echo out 时,(\)
反斜杠会显示向上。
有办法解决这个问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
从 PHP 5.4 开始,json_encode() 可以使用一些常量来按照您想要的方式格式化 json 响应。
要删除反斜杠,请使用:
JSON_UNESCAPED_SLASHES
。像这样:查看 PHP 文档以获取更多常量和更多信息:
http://php.net/manual/ en/function.json-encode.php
JSON 常量列表:
http://php.net/手册/en/json.constants.php
Since PHP 5.4 there are constants which can be used by
json_encode()
to format the json reponse how you want.To remove backslashes use:
JSON_UNESCAPED_SLASHES
. Like so:View the PHP documentation for more constants and further information:
http://php.net/manual/en/function.json-encode.php
List of JSON constants:
http://php.net/manual/en/json.constants.php
如果您使用 PHP 5.2,则 json_encode 在调用时只需要 1 个参数。这是 json 值的转义斜杠的替代方法:
如果您的数据很复杂,请不要使用它。
If you using PHP 5.2, json_encode just expects only 1 parameter when calling it. This is an alternative to unescape slash of json values:
Don't use it if your data is complicated.
确实有效的解决方案是这样的:
但是,您在这里必须非常小心,因为您需要确保所有值的引号都被转义(无论如何,这通常是正确的,但特别是现在您将剥离所有转义) PHP 的愚蠢(且功能失调)的“助手”功能(在所有对象 ID 和值前面添加不必要的反斜杠)。
因此,默认情况下,php 对包含引号的值进行双重转义,因此,如果您的数据库中有
My name is "Joe"
值,php 会将其返回为我的名字是\\“Joe\\”
。这对您可能有用也可能没用。如果不是,您可以采取额外的步骤,替换那里的前导斜杠,如下所示:
是的......它很丑陋......但它有效。
然后你会得到一些与实际 JSON 有点相似的东西。
the solution that does work is this:
However you have to be extremely careful here because you need to make sure that all your values have their quotes escaped (which is generally true anyway, but especially so now that you will be stripping all the escapes from PHP's idiotic (and dysfunctional) "helper" functionality of adding unnecessary backslashes in front of all your object ids and values).
So, php, by default, double escapes your values that have a quote in them, so if you have a value of
My name is "Joe"
in your DB, php will bring this back asMy name is \\"Joe\\"
.This may or may not be useful to you. If it's not you can then take the extra step of replacing the leading slash there like this:
yeah... it's ugly... but it works.
You're then left with something that vaguely resembles actual JSON.
我刚刚发现
json_encode
仅在单引号内使用时才会转义\n
。和
I just figured out that
json_encode
does only escape\n
if it's used within single quotes.And
更简单的方法是
Simpler way would be
是的,这是可能的。看!
但你为什么要这么做呢?
Yes it's possible. Look!
But why would you want to?
正如 HungryDB 所说,更简单的方法是:
查看您的 php 版本,因为此参数已在版本 5.4.0 中添加
json_encode 文档
As HungryDB said the easier way for do that is:
Have a look at your php version because this parameter has been added in version 5.4.0
json_encode documentation
我使用以下命令删除斜杠
I use the following to remove the slashes
您不想删除它。因为 JSON 对字符串使用双引号“”,而您返回的
这些反斜杠会转义这些引号
You do not want to delete it. Because JSON uses double quotes " " for strings, and your one returns
these backslashes escape these quotes
删除斜杠的最佳方法是在 json_decode() 中使用 JSON_UNESCAPED_SLASHES 标志。
示例:
您可以在 json_encode() 中使用多个标志。
例子:
The best way you can remove slashes using JSON_UNESCAPED_SLASHES flag inside json_decode()
Example:
You can use multiple flags in json_encode().
Example:
所有建议使用
JSON_UNESCAPED_SLASHES
的答案都是完全错误的,因为此标志不会影响 反斜杠 (\
) 转义,只会影响通常的 斜杠 (/
)。实际上,唯一的选择是将结果字符串中的双反斜杠替换为单反斜杠,如下所示:
这将输出以下内容:
All answers with advice to use
JSON_UNESCAPED_SLASHES
is totally wrong, as this flag doesn't affect backslashes (\
) escaping, only usual slashes (/
).Actually, the only option is to replace double backslashes to single backslashes in the resulting string, like this:
This will output following: