我需要转义json编码的字符串。
我在字符串下面有encode
。
json_encode(['test1' => '','test2' => '','test3' => ''])
它将存储在mysql
表格中,
"{\"test1\":\"\",\"test2\":\"\",\"test3\":\"\"}"
我想存储像,
{"test":"","test2":"","test3":""}
谢谢!
答案 0 :(得分:2)
尝试以下代码
<?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":""}
?>
答案 1 :(得分:1)
添加JSON_UNESCAPED_SLASHES
作为最后一个参数
echo json_encode(['test1' => '','test2' => '','test3' => ''],JSON_UNESCAPED_SLASHES)
输出
{"test1":"","test2":"","test3":""}
答案 2 :(得分:0)
您可以使用stripslashes功能
$string = '{\"title\":\"\",\"description\":\"\",\"keywords\":\"\"}';
echo stripslashes($string);
您也可以使用preg_replace
<?php
$string = '{\"title\":\"\",\"description\":\"\",\"keywords\":\"\"}';
echo $new_str = preg_replace('~[\\\\/*?<>|]~', '', $string);
?>
输出:
{ "title ": "", "description ": "", "keywords ": ""}