Skip to content

Instantly share code, notes, and snippets.

@mariomka
Last active July 20, 2017 18:16
Show Gist options
  • Save mariomka/9ff7f271e0441128fc90834d2e83c7f3 to your computer and use it in GitHub Desktop.
Save mariomka/9ff7f271e0441128fc90834d2e83c7f3 to your computer and use it in GitHub Desktop.
Madrid Devs regex challenge :O
<?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