Search code examples
phpjsonparser-generator

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 \{ \, \} \:


Solution

  • 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