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

发布于 2024-12-02 18:42:34 字数 336 浏览 3 评论 0原文

如何删除字符串上的(\)反斜杠?当使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

梦在深巷 2024-12-09 18:42:34
json_encode($response, JSON_UNESCAPED_SLASHES);
json_encode($response, JSON_UNESCAPED_SLASHES);
世态炎凉 2024-12-09 18:42:34

从 PHP 5.4 开始,json_encode() 可以使用一些常量来按照您想要的方式格式化 json 响应。

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

json_encode($response, 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:

json_encode($response, JSON_UNESCAPED_SLASHES);

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

单身情人 2024-12-09 18:42:34

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

stripslashes(json_encode($array))

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

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:

stripslashes(json_encode($array))

Don't use it if your data is complicated.

删除→记忆 2024-12-09 18:42:34

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

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

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

因此,默认情况下,php 对包含引号的值进行双重转义,因此,如果您的数据库中有 My name is "Joe" 值,php 会将其返回为
我的名字是\\“Joe\\”

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

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

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

然后你会得到一些与实际 JSON 有点相似的东西。

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

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\\".

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.

思念绕指尖 2024-12-09 18:42:34

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

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

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

I just figured out that json_encode does only escape \n if it's used within single quotes.

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

And

echo json_encode('Hello World\n');
// results in "Hello World\\\n"
誰ツ都不明白 2024-12-09 18:42:34

更简单的方法是

$mystring = json_encode($my_json,JSON_UNESCAPED_SLASHES);

Simpler way would be

$mystring = json_encode($my_json,JSON_UNESCAPED_SLASHES);
春庭雪 2024-12-09 18:42:34

是的,这是可能的。看!

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

但你为什么要这么做呢?

Yes it's possible. Look!

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

But why would you want to?

你怎么这么可爱啊 2024-12-09 18:42:34

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

$mystring = json_encode($my_json,JSON_UNESCAPED_SLASHES);

查看您的 php 版本,因为此参数已在版本 5.4.0 中添加

json_encode 文档

As HungryDB said the easier way for do that is:

$mystring = json_encode($my_json,JSON_UNESCAPED_SLASHES);

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

json_encode documentation

身边 2024-12-09 18:42:34

我使用以下命令删除斜杠

echo json_decode($mystring, JSON_UNESCAPED_SLASHES);

I use the following to remove the slashes

echo json_decode($mystring, JSON_UNESCAPED_SLASHES);
执笏见 2024-12-09 18:42:34

您不想删除它。因为 JSON 对字符串使用双引号“”,而您返回的

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

这些反斜杠会转义这些引号

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

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

these backslashes escape these quotes

对风讲故事 2024-12-09 18:42:34

删除斜杠的最佳方法是在 json_decode() 中使用 JSON_UNESCAPED_SLASHES 标志。

示例:

echo json_encode($str, JSON_UNESCAPED_SLASHES);

您可以在 json_encode() 中使用多个标志。

例子:

json_encode($str, JSON_FORCE_OBJECT | JSON_UNESCAPED_SLASHES)

The best way you can remove slashes using JSON_UNESCAPED_SLASHES flag inside json_decode()

Example:

echo json_encode($str, JSON_UNESCAPED_SLASHES);

You can use multiple flags in json_encode().

Example:

json_encode($str, JSON_FORCE_OBJECT | JSON_UNESCAPED_SLASHES)
爱的十字路口 2024-12-09 18:42:34

所有建议使用 JSON_UNESCAPED_SLASHES 的答案都是完全错误的,因为此标志不会影响 反斜杠 (\) 转义,只会影响通常的 斜杠 (/)。

实际上,唯一的选择是将结果字符串中的双反斜杠替换为单反斜杠,如下所示:

$str      = 'some \n text';

$encoded  = json_encode($str, JSON_UNESCAPED_SLASHES);
$replaced = str_replace('\\\\', '\\', $encoded);

echo 'Backslashes doubled: ' . $encoded . '<br>';
echo 'Duplicates replaced: ' . $replaced;

这将输出以下内容:

Backslashes doubled: "some \\n text"
Duplicates replaced: "some \n text"

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:

$str      = 'some \n text';

$encoded  = json_encode($str, JSON_UNESCAPED_SLASHES);
$replaced = str_replace('\\\\', '\\', $encoded);

echo 'Backslashes doubled: ' . $encoded . '<br>';
echo 'Duplicates replaced: ' . $replaced;

This will output following:

Backslashes doubled: "some \\n text"
Duplicates replaced: "some \n text"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文