0
votes

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

?>
3
Could you add your current result?LoïcR
Hi - Currently nothing gets displayed. I think the 2nd explode needs another loop to go through? Thanks.S Kro
From your sample data above your $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 Cloete
Hi Sam - Actually the sample data did not paste properly. There are big SPACES after 1st, 2nd and 3rd column of data. Regular spaces after that. Thanks.S Kro
@SKro, you can use file of PHPMurad Hasan

3 Answers

1
votes

This code works for the input file:

<?php
$rows = explode("\n", file_get_contents('SAMPLE.TXT'));
$result = array();

foreach ($rows as $row) {
    if (trim($row) == "") {
        continue;
    }
    $timeMatches = array();
    $reTime = "/([0-9-]* [0-9:]*) /";
    preg_match($reTime, $row, $timeMatches);
    $re = "/src=(.*) dst=(.*) mac=(.*) user=(.*) agent=(.*) request: (.*) (.*)/";
    $matches = array();
    preg_match($re, $row, $matches);
    $result[] = array('time' => $timeMatches[1], 'src' => $matches[1]
                , 'dst' => $matches[2], 'mac' => $matches[3]
                , 'user' => $matches[4], 'agent' => $matches[5]
                , 'method' => $matches[6], 'url' => $matches[7]);
}

var_dump($result); 

The output of the var_dump($result) is:

array(2) {
[0]=>
  array(8) {
    ["time"]=>
    string(20) "2016-02-24 13:54:23"
    ["src"]=>
    string(18) "172.16.41.15:62490"
    ["dst"]=>
    string(16) "144.76.76.148:80"
    ["mac"]=>
    string(17) "00:1B:0D:63:84:00"
    ["user"]=>
    string(49) "CN=Smith\John,OU=S-HS,OU=SAcc,DC=abc,DC=org,DC=ab"
    ["agent"]=>
    string(76) "'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 seb/2.0 SEBKEY'"
    ["method"]=>
    string(3) "GET"
    ["url"]=>
    string(63) "http://something.com/theme/image.php/clean/page/1455532301/icon"
  }
  [1]=>
  array(8) {
    ["time"]=>
    string(20) "2016-02-24 13:54:23"
    ["src"]=>
    string(18) "172.16.41.15:62485"
    ["dst"]=>
    string(16) "144.76.76.148:80"
    ["mac"]=>
    string(17) "00:1B:0D:63:84:00"
    ["user"]=>
    string(49) "CN=Smith\John,OU=S-HS,OU=SAcc,DC=abc,DC=org,DC=ab"
    ["agent"]=>
    string(76) "'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 seb/2.0 SEBKEY'"
    ["method"]=>
    string(3) "GET"
    ["url"]=>
    string(71) "http://somethingelse.com/theme/image.php/clean/core/1455532301/f/pdf-24"
  }
}
0
votes

I think this should work:

$txt_file    = file_get_contents('C:\sample.txt');
$rows        = explode("\n", $txt_file);
array_shift($rows);

$info = [];
foreach($rows as $row => $data)
{
    //get row data
    $row_data = explode('   ', $data);   //chop each row first based on bigger space

  //--------------------------
    $info[$row] = [];
    list($info[$row]['timestamp'], $info[$row]['ip'],$info[$row]['other'] ) = explode(" ", $row_data[0]);

   // $info[$row]['localinfo']         = $row_data[1];

  //--------------------------

    $row_data1 = explode(' ', $row_data[1]);   //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

?>
0
votes
<?php
$myfile = fopen("C:\sample.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
  echo $line = fgets($myfile) . "<br>";// you can do the explode and assignment here.
    //example
    $row_data = explode(' ', $line);
    //don't worry about spaces, it will trim by PHP `trim` function, that will erase all the spaces
}
fclose($myfile);
?>