Last active
January 25, 2025 08:03
-
-
Save zkelo/f8696ffb59bf8ee6d4831f46b0200001 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Внимание! Кодировку файлов логов предварительно нужно изменить с Windows-1251 (CP866) на UTF-8 без BOM | |
$files = glob('ent_*.log'); | |
echo 'Найдены логи:', PHP_EOL; | |
$inputs = array_map(function ($filepath) { | |
echo '- ', $filepath, PHP_EOL; | |
return fopen($filepath, 'r'); | |
}, $files); | |
$output = fopen('ent_out.csv', 'w'); | |
$re = '/^\[(?P<month>\d{2})\/(?P<day>\d{2})\/(?P<year>\d{2})\s(?P<hours>\d{2}):(?P<minutes>\d{2}):(?P<seconds>\d{2})\]\s\[INFO\]\s\[(?P<zone_name>\w+)\s(?P<zone_id>\d+)\]\s(?P<nickname>.+)\[\d+\](?:\s\(acid\s(?P<account_id>\d+)\))?\sпрошел\sза\s\((?P<time>[\d:]+)\)\.(?:\s\(СВУП\s(?P<lat>[\d:\.]+)\)\.)?(?:\sПадения\s(?P<falls>\d+)\.)?$/i'; | |
$separator = ';'; | |
fputcsv($output, [ | |
'date(DD.MM.YYYY)', | |
'time(HH:MM:SS)', | |
'zone', | |
'zone_id', | |
'player name', | |
'accid', | |
'time(orig)', | |
'time(sec)', | |
'LAT(orig)', | |
'LAT(sec)', | |
'falls' | |
], $separator); | |
echo PHP_EOL, 'Выполняется обработка. Пожалуйста, подождите...', PHP_EOL; | |
echo 'Ниже будут выведены строки, которые не попадают под регулярку (если они есть)', PHP_EOL, PHP_EOL; | |
foreach($inputs as $file) { | |
while ($line = fgets($file)) { | |
$line = trim($line); | |
preg_match($re, $line, $match); | |
if (empty($match)) { | |
$meta = stream_get_meta_data($file); | |
echo '[', $meta['uri'], '] ', $line, PHP_EOL; | |
continue; | |
} | |
$origTime = $match['time']; | |
$match['time'] = explode(':', $match['time']); | |
$origLAT = ''; | |
$lat = ''; | |
if (isset($match['lat']) && stripos($match['lat'], ':') >= 0) { | |
$origLAT = $match['lat']; | |
$match['lat'] = explode(':', $match['lat']); | |
if (isset($match['lat'][1])) { | |
$match['lat'][1] = explode('.', $match['lat'][1]); | |
$match['lat'] = [ | |
$match['lat'][0], | |
$match['lat'][1][0] | |
]; | |
$lat = $match['lat'][0] * 60 + $match['lat'][1]; | |
} | |
} | |
$time = $match['time'][0] * 60 + $match['time'][1]; | |
fputcsv($output, [ | |
implode('.', [ | |
$match['day'], | |
$match['month'], | |
$match['year'] | |
]), | |
implode(':', [ | |
$match['hours'], | |
$match['minutes'], | |
$match['seconds'] | |
]), | |
$match['zone_name'], | |
$match['zone_id'], | |
$match['nickname'], | |
$match['account_id'] ?? '', | |
$origTime, | |
$time, | |
$origLAT, | |
$lat, | |
$match['falls'] ?? -1 | |
], $separator); | |
} | |
fclose($file); | |
} | |
fclose($output); | |
echo PHP_EOL, 'Обработка завершена', PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment