3

I need to escape json encoded string.

I have encode below string.

json_encode(['test1' => '','test2' => '','test3' => ''])

It will store in mysql table like,

"{\"test1\":\"\",\"test2\":\"\",\"test3\":\"\"}"

I want to store like,

{"test":"","test2":"","test3":""}

Thank you !

14
  • 5
    Why don't just decode it when you fetch it? since it's a json, you still need to decode it even though you didn't want to escape it when inserting on a database. Commented Jan 3, 2018 at 7:05
  • 1
    use stripslashes Commented Jan 3, 2018 at 7:07
  • 1
    eval.in/929761 Commented Jan 3, 2018 at 7:07
  • 2
    I think there's a misconception about escaping here. You really don't need to store it escaped. Commented Jan 3, 2018 at 7:13
  • 1
    It's better but it still doesn't change the fact you don't need to store it escaped. Just store it as is and decode it when you read it from the DB. That question is not a real one, you are just trying to solve an issue that is not one in the first place, in a convoluted way on top of that. Commented Jan 3, 2018 at 7:21

3 Answers 3

2

Try below code

<?php
$json = json_encode(['test1' => '','test2' => '','test3' => '']);
$dbjson = "{\"title\":\"\",\"description\":\"\",\"keywords\":\"\"}";

echo "<pre>";
$value = json_encode(json_decode($dbjson));
print_r($value); // output : {"title":"","description":"","keywords":""}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

You need to escape quotes to write your string, but the actual string doesn't contain the escape characters so the decode/encode is just a waste of time and you could just print $dbjson and get the same result
1

Add JSON_UNESCAPED_SLASHES as last argument

echo json_encode(['test1' => '','test2' => '','test3' => ''],JSON_UNESCAPED_SLASHES)

Output

{"test1":"","test2":"","test3":""}

3 Comments

JSON_UNESCAPED_SLASHES won't escape forward slashes, I don't see any link with the original question
@Capsule Who said to escape slash. It's before saving DB.
json_encode(['test1' => '','test2' => '','test3' => ''],JSON_UNESCAPED_SLASHES) and json_encode(['test1' => '','test2' => '','test3' => '']) will both output exactly the same string: {"test1":"","test2":"","test3":""}
0

You can use stripslashes function

$string = '{\"title\":\"\",\"description\":\"\",\"keywords\":\"\"}';
echo stripslashes($string);

And also you can Use preg_replace

<?php
 $string = '{\"title\":\"\",\"description\":\"\",\"keywords\":\"\"}';
 echo $new_str = preg_replace('~[\\\\/*?<>|]~', '', $string);
?>

Output :

{ "title ": "", "description ": "", "keywords ": ""}

4 Comments

Nice copy Paste. In your first answer you're showing { "title ": "", "description ": "", "keywords ": ""} and second one 'test1' => '','test2' => '','test3' => ''
Now it's fine ? @AbdullaNilam :)
Thanks @Capsule, Right Stripslashes function is use for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.