你的 JSON 有JSON_ERROR_SYNTAX
,所以你有错误
选项 A 你应该stripslashes
先使用,但你会失去反斜杠\
$config = '{"regex": "/font-size:\s*(?:.*);/i"}';
$config = json_decode(stripslashes($config),true);
var_dump($config);
输出
array
'regex' => string '/font-size:s*(?:.*);/i' (length=22)
选项 B创建自己的函数
$config = '{"regex": "/font-size:\s*(?:.*);/i"}';
var_dump(splitJSONObject($config));
输出
array
'regex' => string '/font-size:\s*(?:.*);/i' (length=23)
使用的函数(仅拆分为数组)
function splitJSONObject($json) {
$json = str_replace(array("\\\\","\\\""), array("\","""), $json);
$parts = preg_split("@(\"[^\"]*\")|([\[\]\{\},:])|\s@is", $json, - 1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
foreach ( $parts as $index => $part ) {
if (strlen($part) == 1) {
switch ($part) {
case "[" :
case "{" :
$parts[$index] = "array(";
break;
case "]" :
case "}" :
$parts[$index] = ")";
break;
case ":" :
$parts[$index] = "=>";
break;
case "," :
break;
default :
return null;
}
} else {
if ((substr($part, 0, 1) != "\"") || (substr($part, - 1, 1) != "\"")) {
return null;
}
}
}
$json = str_replace(array("\",""","$"), array("\\\\","\\\"","\\$"), implode("", $parts));
return eval("return $json;");
}