32位服务器上的PHP json_decode [英] PHP json_decode on a 32bit server
问题描述
我正在编写一个Twitter混搭服务.当我收到json数据时,某些twit id大于 2147483647 (这是32位服务器上允许的最大整数).
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).
我想出了一个可行的解决方案,即将整数转换为字符串;这样,json_decode()函数在尝试生成数组时就不会有任何问题.
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.
这是我需要实现的:
之前(原始JSON数据)
[{"name":"john","id":5932725006},{"name":"max","id":4953467146}]
之后(应用解决方案)
[{"name":"john","id":"5932725006"},{"name":"max","id":"4953467146"}]
我正在考虑一个preg_match实现,但是我不知道如何做到防弹.任何帮助将不胜感激.
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.
推荐答案
您可以使用 preg_replace
捕获数字并添加引号,如下所示:
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"}]
尝试上述示例此处.
这篇关于32位服务器上的PHP json_decode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!