tags:

views:

34

answers:

2

I have a set of data that looks like this when using print_r($var):

cbfunc({"query":{"count":"12","created":"2010-06-11T01:20:19Z","lang":"en-US"},"results":["\n 238.l.739089.t.4<\/team_key>\n 4<\/team_id>\n CHEE-HOO!!!<\/name>"]}); 

It looks like JSON to me, so I've tried to use json_decode but can't get it right. My goal is to print the xml data found in "results".

Any helpful pointers would be greatly appreciated.

+1  A: 

It looks like it is wrapped in a callback cbfunc. so you need to strip that out before you can run json_decode on it.

try

$decode_this = substr($var, 6, -1);

You don't show the end of the responseText but the snippet above should give you everything between the start of the callback 'cbfunc(' and the last char, exclusive. You may have to change it to -2 if there is also a ; etc.

unomi
Thanks for your reply. I tried what you suggested but still had no luck.Though I did find a solution to convert JSONP to JSON: $var=preg_replace('/.+?({.+}).+/','$1',$var);
brant
A: 

Thanks to ZZ Coder's response, I figured out the solution.

According to a comment on the json function at PHP. The JSONP needs to be converted to JSON (without padding) via a handy preg_replace...

$var=preg_replace('/.+?({.+}).+/','$1',$var); 

Then, the JSON can be parsed to print the results data:

$obj = json_decode($var, true);
print $obj["results"][0];
brant