6

im writting a twitter mashup service. When i receive the json data, some of the twit ids are greater than 2147483647 (which is the maximum allowed integer on 32bit servers).

I came up with a solution that works, which is converting the integers to strings; that way the json_decode() function won't have any problems when trying to generate the array.

This is what i need to achieve:

Before (original JSON data)

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

After (solution applied)

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

I'm thinking of a preg_match implementation, but i have no idea on how to do it bullet-proof. Any help will be much appreciated.

Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
Andres SK
  • 10,779
  • 25
  • 90
  • 152

3 Answers3

12

You can use preg_replace to capture the numbers and add the quotes, something like this:

$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.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 2
    You'll want `"id":` in that expression, surely? Otherwise it will match any sequence of numbers, badly breaking any string that contains a digit. – bobince Nov 22 '09 at 01:08
  • exactly. it works fine... but it would be better if it would only match the digits near "id": – Andres SK Nov 22 '09 at 01:11
  • in fact... if there is "name":"junior2" it will become "junior"2"" with your code :s – Andres SK Nov 22 '09 at 01:15
  • 2
    Yeah, bobince is right. Try `preg_replace('/"id":(\d+)/', '"id":"$1"', $jsonString)` – Benji XVI Nov 22 '09 at 01:16
  • Thx. i think some older PHP version may have this issue, while new do not. becoz i got a working json_decode in one machine, but not the one with php 5.2.3, at least. but the above solution works! – joetsuihk Jan 20 '10 at 15:27
1

If you use PHP 5.2 those long ids will be parsed into a float, which though not ideal at least gives you another 21 bits of integer precision, which should easily be enough to store those ids. (A 64-bit server would be ideal of course.)

bobince
  • 528,062
  • 107
  • 651
  • 834
  • the server where this is running has both php4 and php5 environments. in order to activate php5 i have to set this flag in the .htaccess file: AddType application/x-httpd-php5 .php Maybe this weird setting won't let me get the int2float feature. Thanks for the info though. – Andres SK Nov 22 '09 at 01:21
  • That should work, but you may, alas, still be running an older version of PHP5 too. – bobince Nov 22 '09 at 01:27
0

If it comes down to it, you can attempt to use the big_int PECL extension. This lets PHP use numbers that are extraordinarily large, should you need to. It's a big leap, but if you're routinely dealing with numbers that border on the edge of mindnumbingness, you'll likely find it helpful.

mattbasta
  • 13,492
  • 9
  • 47
  • 68