php - Reverse reading a text file till a random date is encountered -
i have text file like:
================================================ [feb 11 2013 13:17:14] - string here , here general options - [something] line y ================================================ line 1 line 2 line 3 line 4 line 5 here. error message: referenceerror: applymetadatatemplate undefined; line: 625 line 7 ================================================ [feb 11 2013 16:07:14] - string here , here general options - [something] line y ================================================ line 1 line 2 line 3 line 4 line 5 here. error message: referenceerror: applymetadatatemplate undefined; line: 625 line 7 now want read file backwards , each error find within record new date, need something. need help 1) reading file backwards until encounter date , saving date, 2) grabbing lines until string , finding word error within it. note: each record may have different number of lines , may not have word error within it. it's more "finding , matching date , finding error within record" type of problem.
$matches = array(); $file = file("log.txt"); $file = array_reverse($file); foreach($file $f){ if (stripos($f, "[") !== false) { // check string date regex preg_match('/(\d*) (\d{2}) (\d{4})/', $f, $matches); // check parts of date found if(count($matches) > 2) { echo $f; //print line break; } } }
read file, , convert array, reverse array backward traversal, print out lastly date.
php file array-reverse
No comments:
Post a Comment