更換分組圖案我有,我想用PHP從字符串數組取代「Systems_x0020_Changed_IDs」值的JSON文件。 「39122」變成[39122],並且「39223,39244,39395」變成[39223,39244,39395]。我正在使用http://www.regexpal.com/來測試我的表情。表達式是:問題與PHP的正則表達式
"([(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一樣,只有引號內的數字並忽略其餘部分?
修改與正則表達式結構化數據像JSON是自找麻煩。我建議用'json_decode'將它讀入適當的結構中,修改結構並用'json_encode'將其寫回。 –