json_decode和float值

A friend asked me a simple code to grab values from a website, no problem. This website is using a json API, again, no problem.

But, after parsing results I figured values were all wrong.

Example:

A value on the json is 846.51, but my script is returning 844.71.

My current "code":

$data = file_get_contents('https://blockchain.info/fr/ticker');
$json = json_decode($data);

print_r($json->{'USD'}->{'15m'});

So, I searched and i found out it may be a php bug related to x64 processors, not sure.

Any workaround to fix this ?

So ! It was indeed a php bug according to https://bugs.php.net/bug.php?id=50224

Here is the fixed version:

$data = file_get_contents('https://blockchain.info/fr/ticker');
$res = preg_replace( '/":(\d+)/', '":"\1"', $data );
$json = json_decode($res);

print_r($json->{'EUR'}->{'15m'});

Improving answer from John Konolol : the regex will not work if the value is a floatting decimal number expressed in sci format ("2.038069541E9").

Regex must be :

preg_replace( '/":(\d+\.*\d*E*e*\d*)/', '":"\1"', $data)

It will convert all number to string, including float number (1.34) or sci format numer (1E3) which are valid in Json.