Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to json_encode without escaping slashes?

Tags:

json

php

escaping

I'm encoding it like so..

json_encode($array_list, JSON_UNESCAPED_SLASHES)

Ex: \n turns into \\n, \r\n turns into \\r\\n

But, it's still escaping the slashes! What's wrong and how to fix it? Thanks.

like image 954
unwise guy Avatar asked Feb 08 '13 04:02

unwise guy


1 Answers

I think it is because of single and double quotes, see the examples

$arr = array("\n\r");
echo json_encode($arr,JSON_UNESCAPED_SLASHES);  // ["\n\r"]

$arr = array('\n\r');
echo json_encode($arr,JSON_UNESCAPED_SLASHES);  //["\\n\\r"]

working example http://codepad.viper-7.com/LvWMhq

like image 68
Yogesh Suthar Avatar answered Oct 13 '22 03:10

Yogesh Suthar