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

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

Val
  • 17,336
  • 23
  • 95
  • 144
  • do you have any sway over the process that is serving these strings? – Dancrumb May 23 '11 at 16:40
  • Why? What's wrong with `json_decode`? What's wrong with JSON for that matter? It makes perfect sense: Strings are data, everything else is syntax. Why do you need to make invalid JSON and still treat it like JSON? – Zirak May 23 '11 at 16:44
  • I agree with Dancrumb's answer. Regex alone is not really suited for parsing recursive languages, like your json-variant. – Markus Jarderot May 24 '11 at 11:09
  • @MizardX I know, Just been looking and looking but can't find one, I understood your answer, and being able to use pairs but it wasn't recursive, I even looked into the json_parser trying to understand how it all works, but it seems that they validate each character one by one, – Val May 24 '11 at 11:14

2 Answers2

3

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

Naftali
  • 144,921
  • 39
  • 244
  • 303
  • "*basically I need the same as json_decode() but without the need of the the quotes on BOTH value....*" – Val May 23 '11 at 16:13
  • @Val, what is wrong with using `json_decode`? why reinvent the wheel? – Naftali May 23 '11 at 16:14
  • @neal please read the question especially last paragraph, I am aware of json rules etc...etc... – Val May 23 '11 at 16:14
  • because I need to use it on another project, im not reinventing the wheel just need it slightly different to suite my needs – Val May 23 '11 at 16:16
  • @Val, can you exaplin better **why** you cant use `json_encode/decode`, how would it not *fit your needs* – Naftali May 23 '11 at 16:17
  • @neal look I am aware of all the above, you have mentioned, including the second parameter to make an array etc... slightly irrelevant to what I am asking, I need a parser, or reg expression to split the string, I did explain that json_decode is of no use to me... Sorry if I sound harsh but I dont like re inventing the wheel its just something very complicated that I need this option or nothing – Val May 23 '11 at 16:20
  • http://stackoverflow.com/questions/168171/regular-expression-for-parsing-name-value-pairs ... this is good example it is using = sign instead of : – Val May 23 '11 at 16:31
  • it does not go deep in a tree like ... Man your like that guy from big bang theory always too difficult, – Val May 23 '11 at 16:36
  • @Val I am trying to help. it doesnt not seem as if you want any. goodbye – Naftali May 23 '11 at 16:37
2

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

Dancrumb
  • 26,597
  • 10
  • 74
  • 130