php 如何删除 json_encode() 函数上的反斜杠?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7282755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:23:35  来源:igfitidea点击:

How to remove backslash on json_encode() function?

phpajaxjsonfunctionbackslash

提问by Ryan

How to remove the (\)backslash on a string? when using echo json_encode()?

如何删除(\)字符串上的反斜杠?什么时候使用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.

注意:当你 echo $str 时,不会有问题......但是当你使用 echo out 时json_encode()(\)反斜杠会出现。

Is there a way to solve this?

有没有办法解决这个问题?

回答by Daveloper87

Since PHP 5.4 there are constants which can be used by json_encode()to format the json reponse how you want.

从 PHP 5.4 开始,可以使用常量json_encode()来格式化 json 响应。

To remove backslashes use: JSON_UNESCAPED_SLASHES. Like so:

要删除反斜杠,请使用:JSON_UNESCAPED_SLASHES. 像这样:

json_encode($response, JSON_UNESCAPED_SLASHES);

View the PHP documentation for more constants and further information:

查看 PHP 文档以获取更多常量和更多信息:

http://php.net/manual/en/function.json-encode.php

http://php.net/manual/en/function.json-encode.php

List of JSON constants:

JSON 常量列表:

http://php.net/manual/en/json.constants.php

http://php.net/manual/en/json.constants.php

回答by Rijk

json_encode($response, JSON_UNESCAPED_SLASHES);

回答by Yevgeny Simkin

the solution that does work is this:

有效的解决方案是这样的:

$str = preg_replace('/\\"/',"\"", $str);

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).

但是,在这里您必须非常小心,因为您需要确保所有值的引号都被转义了(无论如何这通常是正确的,但尤其是现在您将从 PHP 的愚蠢(和功能失调)“助手”中剥离所有转义" 在所有对象 ID 和值前面添加不必要的反斜杠的功能)。

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 as My name is \\"Joe\\".

因此,默认情况下,php 会对包含引号的值进行双重转义,因此如果您的数据库中有一个值My name is "Joe",php 会将其作为 My 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:

这对您可能有用也可能没有用。如果不是,您可以采取额外的步骤来替换那里的前导斜杠,如下所示:

$str = preg_replace('/\\\"/',"\"", $str);

yeah... it's ugly... but it works.

是的......它很丑......但它有效。

You're then left with something that vaguely resembles actual JSON.

然后你会得到一些类似于实际 JSON 的东西。

回答by Vuong

If you using PHP 5.2, json_encode just expect only 1 parameter when call it. This is an alternative to unescape slash of json values:

如果您使用 PHP 5.2,则 json_encode 在调用它时只需要 1 个参数。这是对 json 值的 unescape 斜线的替代方法:

stripslashes(json_encode($array))

Don't use it if your data is complicated.

如果您的数据很复杂,请不要使用它。

回答by HungryDB

Simpler way would be

更简单的方法是

$mystring = json_encode($my_json,JSON_UNESCAPED_SLASHES);

回答by totas

I just figured out that json_encodedoes only escape \nif it's used within single quotes.

我刚刚发现json_encode只有\n在单引号内使用时才会转义。

echo json_encode("Hello World\n");
// results in "Hello World\n"

And

echo json_encode('Hello World\n');
// results in "Hello World\\n"

回答by suarsenegger

As HungryDB said the easier way for do that is:

正如 HungryDB 所说,更简单的方法是:

$mystring = json_encode($my_json,JSON_UNESCAPED_SLASHES);

Have a look at your php versionbecause this parameter has been added in version 5.4.0

看看你的php版本,因为这个参数已经在5.4.0版本中添加了

json_encode documentation

json_encode 文档

回答by Yeroon

Yes it's possible. Look!

是的,这是可能的。看!

$str = str_replace('\', '', $str);

But why would you want to?

但你为什么要这样做?

回答by genesis

You do not want to delete it. Because JSON uses double quotes " " for strings, and your one returns

您不想删除它。因为 JSON 使用双引号 " " 作为字符串,而你的返回

"$(\"#output\").append(\"
This is a test!<\/p>\")"

these backslashes escape these quotes

这些反斜杠逃脱了这些引号

回答by n4zg

I use the following to remove the slashes

我使用以下内容删除斜线

echo json_decode($mystring, JSON_UNESCAPED_SLASHES);