32 位服务器上的 PHP json_decode [英] PHP json_decode on a 32bit server

查看:22
本文介绍了32 位服务器上的 PHP json_decode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆