【问题标题】:Complex parsing a text file in PHP在 PHP 中复杂地解析文本文件
【发布时间】:2016-02-25 08:55:11
【问题描述】:

所以我正在尝试解析具有以下格式的 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. 将整个文件解析成一个数组。 //完毕
2。拾取 1 145 之后的所有内容...(将在数组的 [3] 中结束)并进一步解析它,以便我有以下故障。
- 网址
- src=172.16.41.15:62490
- dst=144.76.76.148:80
- mac=00:1B:0D:63:84:00
- 用户=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

?>

【问题讨论】:

  • 您可以添加您当前的结果吗?
  • 嗨 - 目前没有显示任何内容。我认为第二次爆炸需要另一个循环才能通过?谢谢。
  • $row_data 数组上方的示例数据中,其中有两个元素,因为只有一个更大的空白。所以$row_data1 = explode(' ', $row_data[3]);实际上应该是$row_data1 = explode(' ', $row_data[1]);
  • 嗨 Sam - 实际上样本数据没有正确粘贴。第 1、2、3 列数据后有大空格。之后的常规空格。谢谢。
  • @SKro,你可以使用PHP的file

标签: php arrays file parsing text


【解决方案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"
  }
}

【讨论】:

  • 这看起来很有希望 Kordi!将与它一起玩。谢谢。
  • 一个问题 Kordi - 如何访问 $result[ ] 中的各个元素?如 - 数据如何存储在其中?谢谢。
  • 转储了输出,所以很清楚。如果你想访问第一行的方法,只需使用 $result[0]['method']
  • 嗨 kordi - 没关系。我想我明白了。谢谢。
  • 感谢科迪!但是格式好像不对。是否有一个 toTime() 函数可以将其更改回文件中的格式?再次感谢!
【解决方案2】:

我认为这应该可行:

$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

?>

【讨论】:

    【解决方案3】:
    <?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);
    ?> 
    

    【讨论】:

    • 嗨 Frayne - 这看起来既有趣又简单。如何从 $row_data[] 中提取我需要的单行元素?请协助。谢谢。
    • 我想,你只需要explode space。如果你有很大的whitespace 然后从它开始。要么您的重要物品丢失了。
    • @SKro,但是通过这种方式,您也可以做这些事情,而无需像获得$rows那样进行额外的爆炸
    • 嗨 Frayne - 我需要更多帮助。您能否举例说明如何从 row_data 变量中提取单个元素?我需要再次使用“爆炸”吗?还是只使用索引号 [ ] ?谢谢。
    • 知道了。谢谢@Frayne。
    猜你喜欢
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多