开发者

json text split reg expression or parser

$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')
// );

is there开发者_开发问答 a regular expression to split this using preg_split in php?

basically I need the same as json_decode() but without the need of the the quotes on BOTH value and key and the only thing escaped are four characters \{ \, \} \:


Well for one thing that json is incorrect and will spew out an error on json_decode.

read the specs for json here

One correct implementation of the json is:

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

Also json_decode never yields an Array it yields a object(stdClass) unless you add the true parameter


You're probably going to want to look at a parser rather than a regular expression, given the arbitrary nesting that could occur here.

Try:

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

or

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

or

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜