2012-12-21 32 views
0

繼續從Getting the file name from a text file after string matching - PHP 我有格式的log.txt文件讀取錯誤和文件名:從log.txt文件

================================================ 
Header of entry with date and time 
================================================ 
Loaded options from XML file: '/path/to/the/file/some_file.xml 
extendedPrintPDF started 
Postfix '4.4' was append from file 'some-other_file.xml' for file: /path/to/the/file/some-other_file.indd 
printPDF started 
PDF Export Preset: Some preset 
PDF file created: '/path/to/the/file/some_filey_namex_lo_4.4.pdf'. 
File filename_lo-metadata.xml removed 
postprocessingDocument started 
INDD file removed: /path/to/the/file/some_file1_name_lo.indd 
Error opening document: /path/to/the/file/some_file_name_lo.indd: Error: Either the file does not exist, you do not have permission, or the file may be in use by another application; line: 190 
================================================ 
Header of entry with date and time 
================================================ 
Loaded options from XML file: '/path/to/she/file/some_options.xml 
extendedPrintPDF started 
extendedPrintPDF: Error: Unsaved documents have no full name.; line: 332 

我想捕字「錯誤」(如果存在)的日誌條目,然後檢測錯誤消息(如「未保存的文件都沒有全名」),或者只是「錯誤打開文件」,並獲得所有和ONLY正在創建的PDF文件的名稱。

我是一個新手,真的不知道應該怎麼去這樣做。

我想我已經提供了完整的信息,這是我沒在我以前的帖子做。

更改: 我還想從/path/to/the/file/filename.something獲取'the'。 我不是在尋找一個確切的答案(儘管這將是巨大的)。但是,一些幫助或指導將不勝感激。

注: 有沒有辦法做到這一點,不許使用正則表達式(我是一個新手,也,我不是在正則表達式都好) 感謝

+0

最好的方式來獲得良好的正則表達式是坐在一個在線正則表達式測試儀和數字像這樣的事情了:-) – phpisuber01

+0

@Bart:疑難雜症。謝謝。 :) –

回答

0

這確實只匹配有限的日誌的語法片斷你給了我們,但它應該一開始做的。它通過日誌文件運行線由行和會盡力跟蹤哪些過時的部分的它在

<?php 
$logfile = '/path/to/the/file.log'; 
$fh = fopen($logfile, 'r') or die("can't open file."); 

$r_delim = '/^=*$/';   // matches lines of only = 
$r_time = '/^\[(.*)\] - .*$/'; // matches lines that start with [, captures contents between [ and ] 
$r_error = '/ Error: /';  // matches lines that contain the string ' Error: ' 

$skip_delim = FALSE; 
$last_timestamp = 'no stamp'; 
$matches = array(); 

while($line = fgets($fh)) { 
     if(preg_match($r_delim, $line) && ! $skip_delim) { 
       $line = fgets($fh); 
       preg_match($r_time, $line, $matches); 
       $last_timestamp = $matches[1]; 
       $skip_delim = TRUE; 
     } else if(preg_match($r_delim, $line) && $skip_delim) { 
       $skip_delim = FALSE; 
     } else if(preg_match($r_error, $line)) { 
       printf('%s :: %s', $last_timestamp, $line); 
     } 
} 

輸出:

Jan 25 2012 11:26:03 :: Error opening document: /the/path/to/file/some_file_name_lo.indd: Error: Either the file does not exist, you do not have permission, or the file may be in use by another application; line: 190 
Feb 24 2012 15:57:43 :: extendedPrintPDF: Error: Unsaved documents have no full name.; line: 332 
Feb 24 2012 15:57:43 :: extendedPrintPDF: Error: Unsaved documents have no full name.; line: 332 
+0

太好了。但是,這不會使用.pdf擴展名捕捉文件名?我想我可以嘗試phpisuber01的答案來完成這項工作。也會試試這個。非常感謝。 :) –

1

正則表達式

如何獲得文件名:

#(/.*/\w+\.\w{3})# 

如何得到錯誤:

/Error: (.*)/m 

PHP

<?php 

$file_data = __your__file__data__; 

preg_match_all("#(/.*/\w+\.\w{3})#", $file_data, $filenames); 
var_dump($filenames); 

preg_match_all("/Error: (.*)/m", $file_data, $errors); 
var_dump($errors); 

?> 

示例輸出

array (size=2) 
    0 => 
    array (size=3) 
     0 => string '/the/path/of/log_options.xml' (length=28) 
     1 => string '/path/of/some/filesomething.ind' (length=31) 
     2 => string '/the/path/of/log_options.xml' (length=28) 
    1 => 
    array (size=3) 
     0 => string '/the/path/of/log_options.xml' (length=28) 
     1 => string '/path/of/some/filesomething.ind' (length=31) 
     2 => string '/the/path/of/log_options.xml' (length=28) 

array (size=2) 
    0 => 
    array (size=2) 
     0 => string 'Error: file doesnt exist or no permissions 
' (length=44) 
     1 => string 'Error: Unsaved documents have no full name: line xyz' (length=52) 
    1 => 
    array (size=2) 
     0 => string 'file doesnt exist or no permissions 
' (length=37) 
     1 => string 'Unsaved documents have no full name: line xyz' (length=45) 

我會讓你知道如何使用這些信息,並把它放進你的數據庫。

+0

隨時:-),有時沒有避免野獸。 – phpisuber01

+0

對不起,我沒有看到你的名字,並在我的評論中瘋狂起來:(謝謝你一堆。 –

+0

該文件名的RegEx僅返回.indd和.xml文件。但是,我需要.pdf如何修改RegEx以僅捕獲帶.pdf擴展名的文件名? 另外,我需要從/ path/of/file部分捕獲'path'。你如何建議我去做這件事? ? PS你不需要給我正確的答案。如果你能指導我的話,那將是可以的。:) –