我有一个JSON文件,我想用PHP来替换" Systems_x0020_Changed_IDs"从字符串到数组的值。 " 39122"成为[39122]和" 39223,39244,39395"变成[39223,39244,39395]。我正在使用http://www.regexpal.com/来测试我的表情。表达式是:
"([(0-9)+((, *))]+)+"
这在PHP中产生了意想不到的结果。在我的JSON文件中:
[{ "ID": 1050436, "Title": "THE SKY IS FALLING!!!!", "Application_x0020_ID": 242, "Systems_x0020_Changed": "Academic Planning System (APS),\"Documents planning and evaluation processes at UGA that support cont", "Systems_x0020_Changed_IDs": "39122", "Status": "New", "Modified": "2015-10-28T16:14:45.573-04:00", "Age": 40, "Description_x0020__x0028_Public_x0029_": "I'm chicken little and the SKY IS FALLING!", "Impact_x0020__x0028_Public_x0029_": "The world is going to end!", "Start_x0020_Time": "2015-10-28T00:00:00-04:00", "End_x0020_Time": "2015-10-30T00:00:00-04:00", "Hours": 12 }, { "ID": 1050740, "Title": "This is a Title", "Application_x0020_ID": 242, "Systems_x0020_Changed": "EITS Websites,\"EITS departmental web pages.\", GACRC Archival Storage,\"Archival Storage for Research Data\", VPS,\"Mainframe distributed printing system\"", "Systems_x0020_Changed_IDs": "39223, 39244, 39395", "Status": "New", "Modified": "2015-11-05T17:31:13.15-05:00", "Age": 32, "Description_x0020__x0028_Public_x0029_": "We will tell jokes to the clients", "Impact_x0020__x0028_Public_x0029_": "Everyone will notice the change.", "Start_x0020_Time": "2015-11-27T08:38:00-05:00", "End_x0020_Time": "2015-11-30T00:00:00-05:00", "Hours": 1 }]
行末尾的几个逗号被替换为方括号[],以便输出如下:
[{ "ID": 1050436, "Title": "THE SKY IS FALLING!!!![,]Application_x0020_ID": 242, "Systems_x0020_Changed": "Academic Planning System (APS),\"Documents planning and evaluation processes at UGA that support cont[,]Systems_x0020_Changed_IDs": 39122, "Status": "New[,]Modified": "2015-10-28T16:14:45.573-04:00[,]Age": 40, "Description_x0020__x0028_Public_x0029_": "I'm chicken little and the SKY IS FALLING![,]Impact_x0020__x0028_Public_x0029_": "The world is going to end![,]Start_x0020_Time": "2015-10-28T00:00:00-04:00[,]End_x0020_Time": "2015-10-30T00:00:00-04:00[,]Hours": 12 }, { "ID": 1050740, "Title": "This is a Title[,]Application_x0020_ID": 242, "Systems_x0020_Changed": "EITS Websites,\"EITS departmental web pages.\", GACRC Archival Storage,\"Archival Storage for Research Data\", VPS,\"Mainframe distributed printing system\"[,]Systems_x0020_Changed_IDs": [39223, 39244, 39395], "Status": "New[,]Modified": "2015-11-05T17:31:13.15-05:00[,]Age": 32, "Description_x0020__x0028_Public_x0029_": "We will tell jokes to the clients[,]Impact_x0020__x0028_Public_x0029_": "Everyone will notice the change.[,]Start_x0020_Time": "2015-11-27T08:38:00-05:00[,]End_x0020_Time": "2015-11-30T00:00:00-05:00[,]Hours": 1 }]
我的问题是,我如何修改表达式,以便PHP的行为类似于regexpal.com,只能在引号内得到数字并忽略其余部分?
答案 0 :(得分:2)
你的正则表达式很奇怪,你似乎试图在一个字符类[...]
中放置一个模式表达式,这可能没有达到你所期望的效果。此外,您的正则表达式将匹配其他键/值对内的值。请尝试这样做,它只匹配键“Systems_x0020_Changed_IDs”的值:
"Systems_x0020_Changed_IDs":\s+"([^"]*)"
答案 1 :(得分:1)
如何将其解析为JSON呢?
$jsons = array('{
"ID": 1050436,
"Title": "THE SKY IS FALLING!!!!",
"Application_x0020_ID": 242,
"Systems_x0020_Changed": "Academic Planning System (APS),\"Documents planning and evaluation processes at UGA that support cont",
"Systems_x0020_Changed_IDs": "39122",
"Status": "New",
"Modified": "2015-10-28T16:14:45.573-04:00",
"Age": 40,
"Description_x0020__x0028_Public_x0029_": "I\'m chicken little and the SKY IS FALLING!",
"Impact_x0020__x0028_Public_x0029_": "The world is going to end!",
"Start_x0020_Time": "2015-10-28T00:00:00-04:00",
"End_x0020_Time": "2015-10-30T00:00:00-04:00",
"Hours": 12
}', '{
"ID": 1050740,
"Title": "This is a Title",
"Application_x0020_ID": 242,
"Systems_x0020_Changed": "EITS Websites,\"EITS departmental web pages.\", GACRC Archival Storage,\"Archival Storage for Research Data\", VPS,\"Mainframe distributed printing system\"",
"Systems_x0020_Changed_IDs": "39223, 39244, 39395",
"Status": "New",
"Modified": "2015-11-05T17:31:13.15-05:00",
"Age": 32,
"Description_x0020__x0028_Public_x0029_": "We will tell jokes to the clients",
"Impact_x0020__x0028_Public_x0029_": "Everyone will notice the change.",
"Start_x0020_Time": "2015-11-27T08:38:00-05:00",
"End_x0020_Time": "2015-11-30T00:00:00-05:00",
"Hours": 1
}');
foreach($jsons as $json){
$json_array = json_decode($json, true);
echo $json_array['Systems_x0020_Changed_IDs'] . "\n";
}
如果您需要正则表达式,您可以执行以下操作:
"Systems_x0020_Changed_IDs":\h*"(([\d+],?\h*)*)"
演示:https://regex101.com/r/yZ6eM3/1
PHP用法:
$string = '{
"ID": 1050436,
"Title": "THE SKY IS FALLING!!!!",
"Application_x0020_ID": 242,
"Systems_x0020_Changed": "Academic Planning System (APS),\"Documents planning and evaluation processes at UGA that support cont",
"Systems_x0020_Changed_IDs": "39122",
"Status": "New",
"Modified": "2015-10-28T16:14:45.573-04:00",
"Age": 40,
"Description_x0020__x0028_Public_x0029_": "I\'m chicken little and the SKY IS FALLING!",
"Impact_x0020__x0028_Public_x0029_": "The world is going to end!",
"Start_x0020_Time": "2015-10-28T00:00:00-04:00",
"End_x0020_Time": "2015-10-30T00:00:00-04:00",
"Hours": 12
}, {
"ID": 1050740,
"Title": "This is a Title",
"Application_x0020_ID": 242,
"Systems_x0020_Changed": "EITS Websites,\"EITS departmental web pages.\", GACRC Archival Storage,\"Archival Storage for Research Data\", VPS,\"Mainframe distributed printing system\"",
"Systems_x0020_Changed_IDs": "39223, 39244, 39395",
"Status": "New",
"Modified": "2015-11-05T17:31:13.15-05:00",
"Age": 32,
"Description_x0020__x0028_Public_x0029_": "We will tell jokes to the clients",
"Impact_x0020__x0028_Public_x0029_": "Everyone will notice the change.",
"Start_x0020_Time": "2015-11-27T08:38:00-05:00",
"End_x0020_Time": "2015-11-30T00:00:00-05:00",
"Hours": 1
}';
$regex = '/"Systems_x0020_Changed_IDs":\h*"((?:[\d+],?\h*)*)"/';
preg_match_all($regex, $string, $matches);
print_r($matches[1]);
输出:
Array
(
[0] => 39122
[1] => 39223, 39244, 39395
)
答案 2 :(得分:0)
我正在寻找的答案是:
$str = preg_replace('/"((\d+[, ]*)+)"/', "[$1]", $str);
我需要JSON文件,除了数字值作为字符串。在我玩了一点之后,我的正则表达式起作用了。