So I am trying to parse a TXT file which has the following format. Each entry is on a single line.
SAMPLE.TXT
2016-02-24 13:54:23 Local0.Info 172.16.120.4 1 1456311263.500015263 ASD_MX600 urls src=172.16.41.15:62490 dst=144.76.76.148:80 mac=00:1B:0D:63:84:00 user=CN=Smith\John,OU=S-HS,OU=SAcc,DC=abc,DC=org,DC=ab agent='Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 seb/2.0 SEBKEY' request: GET http://something.com/theme/image.php/clean/page/1455532301/icon
2016-02-24 13:54:23 Local0.Info 172.16.120.4 1 1456311263.500097075 ASD_MX600 urls src=172.16.41.15:62485 dst=144.76.76.148:80 mac=00:1B:0D:63:84:00 user=CN=Smith\John,OU=S-HS,OU=SAcc,DC=abc,DC=org,DC=ab agent='Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 seb/2.0 SEBKEY' request: GET http://somethingelse.com/theme/image.php/clean/core/1455532301/f/pdf-24
I need to do the following:
1. Parse the entire file into an array. //DONE
2. Pick up everything after 1 145... (which will end up in [3] of the array) and parse it further so that I have the following breakdowns.
- urls
- src=172.16.41.15:62490
- dst=144.76.76.148:80
- mac=00:1B:0D:63:84:00
- user=CN=Smith\John,OU=S-HS,OU=SAcc,DC=abc,DC=org,DC=ab
- agent='Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 seb/2.0 SEBKEY'
- request: GET
- http://something.com/theme/image.php/clean/page/1455532301/icon
I am having a hard time getting the syntax right for the 2nd parse within the main loop. I get the entire giant section from index 3 [3] and I think I am also using the explode() right to chop it off based on ' ' but then I am lost. How do i get hold of the data as shown above? My code progress so far:
<?php
$txt_file = file_get_contents('C:\sample.txt');
$rows = explode("\n", $txt_file);
array_shift($rows);
foreach($rows as $row => $data)
{
//get row data
$row_data = explode(' ', $data); //chop each row first based on bigger space
//--------------------------
$info[$row]['timestamp'] = $row_data[0];
// $info[$row]['localinfo'] = $row_data[1];
$info[$row]['ip'] = $row_data[2];
$info[$row]['other'] = $row_data[3]; //This is where LONGEST string exists
//--------------------------
$row_data1 = explode(' ', $row_data[3]); //chop index item based on smaller space
$rowd_data2[$row_data1]['urlsflows'] = $row_data1[3];
//display data
// echo 'Row ' . $row . ' TIMESTAMP: ' . $info[$row]['timestamp'] . '<br />';
// echo 'Row ' . $row . ' LOCALINFO: ' . $info[$row]['localinfo'] . '<br />';
// echo 'Row ' . $row . ' IP: ' . $info[$row]['ip'] . '<br />';
//--The line below is where I am lost. Kindly help.
echo $rowd_data2[$row_data1]['urlsflows'];
} //end of for loop
?>
$row_data
array would have two elements in it, as there is only one bigger whitespace. So$row_data1 = explode(' ', $row_data[3]);
should actually be$row_data1 = explode(' ', $row_data[1]);
– Samuel Cloetefile
of PHP – Murad Hasan