Ryan
Ryan

Reputation: 1791

How to remove backslash on json_encode() function?

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?

Upvotes: 52

Views: 166742

Answers (12)

Vuong
Vuong

Reputation: 637

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.

Upvotes: 13

Alex
Alex

Reputation: 11

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"

Upvotes: 0

Saroar Hossain Limon
Saroar Hossain Limon

Reputation: 75

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)

Upvotes: 1

n4zg
n4zg

Reputation: 397

I use the following to remove the slashes

echo json_decode($mystring, JSON_UNESCAPED_SLASHES);

Upvotes: 2

Rijk
Rijk

Reputation: 11301

json_encode($response, JSON_UNESCAPED_SLASHES);

Upvotes: 97

totas
totas

Reputation: 10800

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"

Upvotes: 7

therealbigpepe
therealbigpepe

Reputation: 1559

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

Upvotes: 3

Daveloper87
Daveloper87

Reputation: 716

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

Upvotes: 56

HungryDB
HungryDB

Reputation: 575

Simpler way would be

$mystring = json_encode($my_json,JSON_UNESCAPED_SLASHES);

Upvotes: 6

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28389

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.

Upvotes: 9

Yeroon
Yeroon

Reputation: 3243

Yes it's possible. Look!

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

But why would you want to?

Upvotes: 3

genesis
genesis

Reputation: 50982

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

Upvotes: 1

Related Questions