反向读取文本文件,直到遇到随机日期


Reverse reading a text file till a random date is encountered

我有一个文本文件,如:

================================================
[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。这更像是"查找和匹配日期,然后在该记录中查找错误"类型的问题。

$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;
        }
    }
}

读取一个文件,将其转换为数组,然后反转数组进行反向遍历,然后打印出最后一个日期。