I have the follow string that I need to parse, and then run it through json_decode. My code is as follows. Right now I can’t use json_decode because I am not able to properly parse the string it has more than one instance of json and i need to have the preg_match_all break it up like so {}, {}, {}.
CODE
$source = getHTML($URL); //get page source with curl
preg_match('/var\smessages\s\=\s\[(.*?)\]\;/', $source , $json);
echo $json_final = json_decode($json[0]);
echo $json_final_2 = json_decode($json[1]);
You can use
preg_match_all, accepting the[and]on group too (regex:/var\smessages\s\=\s(\[.*?\])\;/). So you will have zero or more JSON valid string syntax atarray[1](afterjson_decode).Now to you call this values, you can use
foreach:Note that the
json_decodereceivestrueon 2nd arg. It say to this function that need return an array, instead of an object (stdClass).Take a look: http://codepad.viper-7.com/XtAiBH