PHP json_decode on a 32-bit server

im calling twitter mashup service. When I get json data, some of the twit identifiers are greater than 2147483647 (which is the maximum allowable integer on 32 bit servers).

I came up with a solution that works that converts integers to strings; thus, the json_decode () function will not have problems when trying to generate an array.

This is what I need to achieve:

To (JSON source data)

[{"name":"john","id":5932725006},{"name":"max","id":4953467146}] 

After (solution applied)

 [{"name":"john","id":"5932725006"},{"name":"max","id":"4953467146"}] 

I am thinking about implementing preg_match, but I have no idea how to make this bulletproof. Any help would be greatly appreciated.

+6
json php 32-bit
source share
3 answers

You can use preg_replace to capture numbers and add quotes, for example:

 $jsonString = '[{"name":"john","id":5932725006},{"name":"max","id":4953467146}]'; echo preg_replace('/("\w+"):(\d+)/', '\\1:"\\2"', $jsonString); //prints [{"name":"john","id":"5932725006"},{"name":"max","id":"4953467146"}] 

Try the above example here .

+12
source share

If you are using PHP 5.2, then long identifiers will be parsed in float, which, although not ideal, at least gives you another 21 bits of integer precision, which should be enough to store these identifiers. (Of course, a 64-bit server would be ideal.)

+1
source share

If this happens, you can try using the big_int PECL extension. This allows PHP to use numbers that are unusually large if you need to. This is a big leap, but if you regularly practice numbers bordering the edge of your mind, you will most likely find it useful.

0
source share

All Articles