2015-10-12 160 views
1

我有一個日誌文件「file.log」。我試圖解析php中的文件來獲取關鍵值對。解析日誌文件

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" 
    } ] 
    } 

我改變了數值。我試圖通過這個PHP代碼解析它。

<?php 
$myFile = "file.log"; 
$lines = file($myFile); 
foreach ($lines as $no => $ln) { 
$out = explode(":", $ln); 
echo($out[1]); 
echo(trim($out[1])); 
?> 

我得到我的輸出

{ { "PROGRESSING", "PROGRESSING", "2012-09-25", "2012-09-25", "xxxxxxxxxxxx", "xxxxxxxxxx", "xxxxxxxxxxxxxxx",

它源源不絕的..不正確的格式。我希望它成爲關鍵價值對。怎麼做?請大家需要幫助!我還需要檢索它們並使用mysql將其存儲在數據庫中。

+0

它看起來像一個json文件。使用json解析器 – Jens

+0

試一下'json_decode()'。我認爲它是以json格式。 –

+0

沒有。它是一個轉碼作業的日誌文件 –

回答

2

更新:使用此日誌文件樣本作爲參考

$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 
+0

如果我使用file_get_contents($ filename)包含文件,代碼不起作用 –

+0

我將用代碼更新答案以正確解析日誌文件 –

+0

$ string不包含「通知」。字符串不能被編輯,因爲它來自不需要編輯的文件。 –

0

不是一個問題,如果你的JSON字符串是不是在JSON文件..你可以解析它原樣

<?php 
$myFile = "file.log"; 
$json = file_get_contents($myFile); 
$json= explode(":",$json,2); 
$json=$json[1]; 
$obj=json_decode($json,true); 
print_r($obj); 

?> 
+0

否輸出正在顯示。這是空白 –

+0

@伸手可及,現在它會適合你.. –

+0

仍然沒有顯示輸出。謝謝你雖然得到了答案:-) –

0

試試這個:

$myFile = "file.log"; 
$str = file_get_contents($myFile); 
$json = trim(strstr($str, ':'), ': '); 
print_r(json_decode($json));// for Object 
print_r(json_decode($json, true));// for Array 

其實「通知」是防止被解讀爲JSON字符串。由於有效的json應該從花括號或方括號開始,我們首先刪除「通知」,然後從字符串的兩側修剪多餘的空格或雙冒號。因此只剩下有效的JSON。

+0

我不應該刪除它們。這就是我如何得到我的文件。我不應該每次都進去編輯。 –

+0

您不必刪除它們,此代碼將自動刪除它們。它跟蹤雙冒號的第一個出現,然後刪除在這種情況下是「通知」的任何東西。 –

+0

沒有得到任何輸出..雖然得到了答案。謝謝。 –