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

时间:2022-12-21 21:34:16

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时,没有问题……但是当您使用json_encode()进行回显时,(\)反斜杠将会出现。

Is there a way to solve this? Thank you.

有没有办法解决这个问题?谢谢你!

10 个解决方案

#1


10  

json_encode($response, JSON_UNESCAPED_SLASHES);

#2


47  

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

#3


7  

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默认情况下,double会转义引号中的值,所以如果你的DB中有我的名字的值,php就会把这个带回来,因为我的名字是“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相似的东西。

#4


3  

Simpler way would be

简单的方法是

$mystring = json_encode($my_json,JSON_UNESCAPED_SLASHES);

#5


2  

Yes it's possible. Look!

是的,这是可能的。看!

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

But why would you want to?

但你为什么要这样做呢?

#6


2  

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 version because this parameter has been added in version 5.4.0

查看一下php版本,因为这个参数是在5.4.0版本中添加的

json_encode documentation

json_encode文档

#7


2  

I just figured out that json_encode does only escape \n if 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"

#8


1  

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只在调用时只期望一个参数。这是对json值的反斜杠的一种替代方法:

stripslashes(json_encode($array))

Don't use it if your data is complicated.

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

#9


0  

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

这些反斜杠可以转义这些引号

#10


0  

I use the following to remove the slashes

我使用以下命令删除斜线

echo json_decode($mystring, JSON_UNESCAPED_SLASHES);

#1


10  

json_encode($response, JSON_UNESCAPED_SLASHES);

#2


47  

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

#3


7  

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默认情况下,double会转义引号中的值,所以如果你的DB中有我的名字的值,php就会把这个带回来,因为我的名字是“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相似的东西。

#4


3  

Simpler way would be

简单的方法是

$mystring = json_encode($my_json,JSON_UNESCAPED_SLASHES);

#5


2  

Yes it's possible. Look!

是的,这是可能的。看!

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

But why would you want to?

但你为什么要这样做呢?

#6


2  

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 version because this parameter has been added in version 5.4.0

查看一下php版本,因为这个参数是在5.4.0版本中添加的

json_encode documentation

json_encode文档

#7


2  

I just figured out that json_encode does only escape \n if 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"

#8


1  

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只在调用时只期望一个参数。这是对json值的反斜杠的一种替代方法:

stripslashes(json_encode($array))

Don't use it if your data is complicated.

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

#9


0  

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

这些反斜杠可以转义这些引号

#10


0  

I use the following to remove the slashes

我使用以下命令删除斜线

echo json_decode($mystring, JSON_UNESCAPED_SLASHES);