Last active
July 20, 2017 18:16
-
-
Save mariomka/9ff7f271e0441128fc90834d2e83c7f3 to your computer and use it in GitHub Desktop.
Madrid Devs regex challenge :O
This file contains 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 | |
// ini_set('memory_limit', '2G'); | |
$handle = fopen('muestra_log.txt', 'r'); | |
if (!$handle) { | |
die('Error loading file.'); | |
} | |
// $results = []; | |
$keys = [ | |
'RemoteHost', | |
'RemoteLogName', | |
'RemoteUser', | |
'TimeReceived', | |
'RequestMethod', | |
'RequestURL', | |
'RequestHTTP', | |
'Status', | |
'ResponseBytes', | |
'RequestHeaderUserAgent', | |
'Cached', | |
'ContentType', | |
'Node', | |
'RTime', | |
'Company', | |
'Referrer', | |
]; | |
$count = 0; | |
while (($line = fgets($handle)) !== false) { | |
preg_match('/(.*) (.*) (.*) \[(.*)\] "(.*) (.*) (.*)" (\d+|-) (\d+|-) "(.*)" (.*) "(.*)" "(.*)" ([\d\.]+|-) "(\d*|-)" "(.*)"/Ui', $line, $matches); | |
array_shift($matches); | |
$result = array_combine($keys, $matches); | |
// Do something or save for later | |
// $results[] = $result; | |
$count++; | |
} | |
// foreach($results as $result) { | |
// Do something | |
// } | |
echo 'Processed ' . $count . ' lines.' . PHP_EOL; | |
fclose($handle); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment