我有一个文本文件,如:
================================================
[Feb 11 2013 13:17:14] - some string here and here
General options - [something]
Line y
================================================
Line 1
Line 2
Line 3
Line 4
Line 5
Something here. Error message: ReferenceError: applyMetadataTemplate is undefined; line: 625
Line 7
================================================
[Feb 11 2013 16:07:14] - some string here and here
General options - [something]
Line y
================================================
Line 1
Line 2
Line 3
Line 4
Line 5
Something here. Error message: ReferenceError: applyMetadataTemplate is undefined; line: 625
Line 7
现在我想向后阅读这个文件,对于我在一个新日期的记录中找到的每个错误,我需要做点什么。我需要帮助1)向后读取文件,直到我遇到日期并保存该日期,2)抓住所有行直到那时为一个字符串并在其中找到单词Error。 注意:每条记录可能具有不同的行数,并且可能不一定在其中包含单词error。它更像是“找到并匹配日期,然后在该记录中找到错误”类型的问题。
答案 0 :(得分:1)
$matches = array();
$file = file("log.txt");
$file = array_reverse($file);
foreach($file as $f){
if (stripos($f, "[") !== false) {
// Check string for date by regex
preg_match('/(\D*) (\d{2}) (\d{4})/', $f, $matches);
// Check that parts of the date were found
if(count($matches) > 2) {
echo $f; //print the line
break;
}
}
}
读取一个文件,并将其转换为数组,然后反转该数组以进行反向遍历,然后打印出最后一个日期。