json - PHP regex parse data between [ ] -
i've string want convert valid json , json_decode it. here's string looks like:
[ ['test', 'lol'], ['test2', 'lol2'] ] [ ['test32', 'lodl'], ['test32', 'lodl2'] ] [ ['tes23t', 'lodel'], ['testde2', 'lolde2'] ]
i want data between each
[ ]
so result might be:
['test', 'lol'], ['test2', 'lol2'], ['test32', 'lodl'], ['test32', 'lodl2'], ['tes23t', 'lodel'], ['testde2', 'lolde2']
so think need use regex
, preg_split
, here's did:
$jsons = preg_split('/\]\s*(?=\[)/', $data, null); $jsond = ""; foreach ($jsons $json) { $json .= ""; $jsond .= $json; } return $jsond;
but it's not working, still can't have data beetween each [ ]
how can ?
thanks in advance
ps: here's real full string https://paste.ee/r/rn7rk
the string pointed to has valid json on each line. however, lines not represent 1 valid json.
i propose manipulate data in minimal way make whole text json simple regular expression. if original data in $data, create json follows:
$json = preg_replace('/(\])\](\r)\[/', '$1,$2', $data);
this remove both closing bracket @ end of line, , opening 1 @ start of next line. instead comma inserted. result valid json, opening bracket right @ start matches final closing bracket.
i took representative text data:
$data = '[["s","13","shelves_norja","49500","0","1","1","#ffffff,#f7ebbc","beige bookcase","for nic naks , books.","","5","true","-1","false","","1","true","0","0","0","false"],["s","117","table_plasto_round*9","45508","0","2","2","#ffffff,#533e10","round dining table","hip plastic furniture","","-1","false","-1","false","","1","false","0","0","0","false"]] [["s","118","table_plasto_square*9","45508","0","1","1","#ffffff,#533e10","occasional table","hip plastic furniture","","-1","false","-1","false","","1","false","0","0","0","false"],["s","119","chair_plasto*9","45508","0","1","1","#ffffff,#533e10,#ffffff,#533e10","chair","hip plastic furniture","","-1","false","-1","false","","1","false","0","1","0","false"],["s","120","carpet_standard*6","48082","0","3","5","#777777","floor rug","available in variety of colors","","105","true","-1","false","","1","true","1","0","0","false"],["s","121","chair_plasty*1","45508","0","1","1","#ffffff,#8eb5d1,#ffffff,#8eb5d1","plastic pod chair","hip plastic furniture","","-1","false","-1","false","","1","false","0","1","0","false"]]';
it has 2 lines, limit data bit. above code produces result, pretty printed:
[ [ "s", "13", "shelves_norja", "49500", "0", "1", "1", "#ffffff,#f7ebbc", "beige bookcase", "for nic naks , books.", "", "5", "true", "-1", "false", "", "1", "true", "0", "0", "0", "false" ], [ "s", "117", "table_plasto_round*9", "45508", "0", "2", "2", "#ffffff,#533e10", "round dining table", "hip plastic furniture", "", "-1", "false", "-1", "false", "", "1", "false", "0", "0", "0", "false" ], [ "s", "118", "table_plasto_square*9", "45508", "0", "1", "1", "#ffffff,#533e10", "occasional table", "hip plastic furniture", "", "-1", "false", "-1", "false", "", "1", "false", "0", "0", "0", "false" ], [ "s", "119", "chair_plasto*9", "45508", "0", "1", "1", "#ffffff,#533e10,#ffffff,#533e10", "chair", "hip plastic furniture", "", "-1", "false", "-1", "false", "", "1", "false", "0", "1", "0", "false" ], [ "s", "120", "carpet_standard*6", "48082", "0", "3", "5", "#777777", "floor rug", "available in variety of colors", "", "105", "true", "-1", "false", "", "1", "true", "1", "0", "0", "false" ], [ "s", "121", "chair_plasty*1", "45508", "0", "1", "1", "#ffffff,#8eb5d1,#ffffff,#8eb5d1", "plastic pod chair", "hip plastic furniture", "", "-1", "false", "-1", "false", "", "1", "false", "0", "1", "0", "false" ] ]
Comments
Post a Comment