Remove double quote in json_encode()

一个人想着一个人 提交于 2019-11-27 05:20:52

If you add an underscore to your regex at the end it will do it.

$array_final = preg_replace('/"([a-zA-Z]+[a-zA-Z0-9_]*)":/','$1:',$array_final);

I assume that's what that preg_replace is for anyway.

You can use this bellow code to remove quote from numeric value.

http://php.net/manual/en/json.constants.php

It will work >=PHP 5.3.

$encoded = json_encode($data, JSON_NUMERIC_CHECK);

Replace this line:

 $array_final = preg_replace('/"([a-zA-Z]+[a-zA-Z0-9]*)":/','$1:',$array_final);

by:

$array_final = preg_replace('/"([a-zA-Z_]+[a-zA-Z0-9_]*)":/','$1:',$array_final);

Note that the regex class [a-zA-Z] does not match the '_'

You can use $.parseJSON to parse the string and create a Javascript object from it, or better yet use a method like $.getJSON to get it

// use can use addslashes() function for storing in mysql database 
// or remove  slashes  u can use stripslashes() function.

$json_array = array(
'title' => 'Example string\'s with "special" characters'
);

echo $json_decode =addslashes(json_encode($json_array));


output-{\"title\":\"Example string\'s with \\\"special\\\" characters\"}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!