复杂解析PHP中的文本文件

时间:2016-02-25 08:55:11

标签: php arrays file parsing text

所以我试图解析具有以下格式的TXT文件。每个条目都在一行上。

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

我需要做以下事情:
1.将整个文件解析为数组。 // DONE
2。在1 145之后拿起所有东西......(最终将在数组的[3]中)并进一步解析,以便我有以下细分。
  - 网址
  - 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'
  - 要求:GET
  - http://something.com/theme/image.php/clean/page/1455532301/icon

我很难在主循环中获得第二个解析的语法。我从索引3 [3]得到整个巨型部分,我想我也正在使用explode()权利根据' '但后来我迷失了。如何获取如上所示的数据?到目前为止,我的代码进展如下:

<?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 个答案:

答案 0 :(得分:1)

此代码适用于输入文件:

<?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); 

var_dump($ result)的输出是:

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"
  }
}

答案 1 :(得分:0)

我认为这应该有效:

$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

?>

答案 2 :(得分:0)

<?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);
?>