0
$var ="
   { 
        key : { 
            key_deep :  val\{ue   /* should be "val{ue" as { is escaped  */
        } , 
        key2 : value
    }

";
print_r(preg_split('//',$var));
// array( 
//    array( 
//       'key'=> array(
//           'key_deep'=> 'val{ue'
//        )
//    ), 
//    array('key2'=>'value')
// );

是否有正则表达式可以在 php 中使用 preg_split 进行拆分?

基本上我需要与 json_decode() 相同的内容,但不需要BOTH value上的引号,并且key唯一转义的是四个字符\{ \, \} \:

4

2 回答 2

3

一方面,json 不正确,并且会在json_decode.

在此处阅读 json 的规格

json的一种正确实现是:

$var ='
   { 
        "key" : { 
            key_deep :  "val\{ue" 
        } , 
        "key2" : "value"
   }
';

json_decode永远不会产生一个Array它产生一个object(stdClass)除非你添加true参数

于 2011-05-23T16:11:39.043 回答
2

考虑到此处可能发生的任意嵌套,您可能希望查看解析器而不是正则表达式。

尝试:

http://pear.php.net/package/PHP_ParserGenerator/redirected

或者

http://www.hwaci.com/sw/lemon/

或者

http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=php+parser+generator

于 2011-05-23T16:42:11.377 回答