更新:使用此日誌文件樣本作爲參考
$logFile = file_get_contents('logfile.log');
// first we replace all instances of the string "Notification: " with a comma to separate the json objects
$cleanLog = str_replace("Notification: ",",",$logFile);
// next we replace the first comma
$cleanLog = '[' . ltrim($cleanLog,",") . ']';
// construct the list of object
$objects = json_decode($cleanLog);
// use this main loop to iterate over all Notification rows
foreach ($objects as $object){
// write a mysql insert statement here,
// you can address each object and inner members as follows:
print $object->state . PHP_EOL;
print $object->outputKeyPrefix . PHP_EOL;
print $object->outputs[0]->id . PHP_EOL;
print $object->input->key . PHP_EOL;
}
:
logfile.log
Notification: {
"state" : "PROGRESSING",
"version" : "2012-09-25",
"jobId" : "xxxxxxxxx",
"pipelineId" : "xxxxxxxxxxx",
"input" : {
"key" : "upload2/xxxxx",
"frameRate" : "auto",
"resolution" : "auto",
"aspectRatio" : "auto",
"interlaced" : "auto",
"container" : "auto"
},
"outputKeyPrefix" : "output2/test4/",
"outputs" : [ {
"id" : "1",
"presetId" : "xxxxxxxxxxxx",
"key" : "xxxxxxxx",
"rotate" : "auto",
"status" : "Progressing"
} ]
}
Notification: {
"state" : "PROGRESSING",
"version" : "2012-09-25",
"jobId" : "xxxxxxxxx",
"pipelineId" : "xxxxxxxxxxx",
"input" : {
"key" : "upload2/xxxxx",
"frameRate" : "auto",
"resolution" : "auto",
"aspectRatio" : "auto",
"interlaced" : "auto",
"container" : "auto"
},
"outputKeyPrefix" : "output2/test4/",
"outputs" : [ {
"id" : "1",
"presetId" : "xxxxxxxxxxxx",
"key" : "xxxxxxxx",
"rotate" : "auto",
"status" : "Progressing"
} ]
}
Notification: {
"state" : "PROGRESSING",
"version" : "2012-09-25",
"jobId" : "xxxxxxxxx",
"pipelineId" : "xxxxxxxxxxx",
"input" : {
"key" : "upload2/xxxxx",
"frameRate" : "auto",
"resolution" : "auto",
"aspectRatio" : "auto",
"interlaced" : "auto",
"container" : "auto"
},
"outputKeyPrefix" : "output2/test4/",
"outputs" : [ {
"id" : "1",
"presetId" : "xxxxxxxxxxxx",
"key" : "xxxxxxxx",
"rotate" : "auto",
"status" : "Progressing"
} ]
}
「通知:」位後的字符串有效json
。可以按如下方式對其進行分析:
<?php
$string = '{
"state" : "PROGRESSING",
"version" : "2012-09-25",
"jobId" : "xxxxxxxxx",
"pipelineId" : "xxxxxxxxxxx",
"input" : {
"key" : "upload2/xxxxx",
"frameRate" : "auto",
"resolution" : "auto",
"aspectRatio" : "auto",
"interlaced" : "auto",
"container" : "auto"
},
"outputKeyPrefix" : "output2/test4/",
"outputs" : [ {
"id" : "1",
"presetId" : "xxxxxxxxxxxx",
"key" : "xxxxxxxx",
"rotate" : "auto",
"status" : "Progressing"
} ]
}';
// construct object
$object = json_decode($string);
// call each property of the object or inner object
print $object->state . PHP_EOL;
// PROGRESSING
print $object->outputKeyPrefix . PHP_EOL;
// output2/test4/
print $object->outputs[0]->id . PHP_EOL;
// 1
// or, for multiple outputs
foreach ($object->outputs as $output)
print $output->rotate . PHP_EOL;
// auto
它看起來像一個json文件。使用json解析器 – Jens
試一下'json_decode()'。我認爲它是以json格式。 –
沒有。它是一個轉碼作業的日誌文件 –