douchengchen7959 2013-08-27 06:43
浏览 34
已采纳

从某个元素名称解析文本文件,直到使用PHP找到另一个元素名称

The title might be confusing so I will try to be as descriptive as I can with this problem. Here's the text file that is going to be parsed:

server1 2013-08-27 08:42:01

username1   902     1  0 Aug26 ?        00:01:51 Text here

username1  2044     1  0 Jul14 ?        04:54:52 Text here

server2 2013-08-27 08:42:03

username2  2184     1  1 Jul17 ?        10:21:11 Text here

server3 2013-08-27 08:42:05

username3  2225     1  0 Jul17 ?        05:04:25 Text here

server2 2013-08-27 08:42:07

username2 13233     1  0 Jul15 ?        00:15:09 Text here

username2 13233     1  0 Jul15 ?        00:15:09 Text here

server3 2013-08-27 08:42:09

username3  6131     1  0 Jul15 ?        00:22:19 Text here

As you can see, the servers aren't in any specific order. What I would like to acheive is to put the servers and their respective bits of text into arrays. The program would parse the file and whenever it finds "server1" it would put that line and everything below into an array called "server1". When it finds a server with a different name, it would add all the lines of text to another array eg. "server2" or "server3".

The ouput would be something similar to:

//serverX where X is 1-99

array(n){
    [0]=> string(n) "serverX ..."
    [1]=> string(n) "usernameX ..."
    [2]=> string(n) "usernameX ..."
    [3]=> string(n) "usernameX ..."
    [4]=> string(n) "serverX ..."
    [5]=> string(n) "usernameX ..."
}

This means all the servers (server1..n) would have ONE array where strings are added to.

  • 写回答

1条回答 默认 最新

  • duan00529 2013-08-27 07:27
    关注

    I suggest to use regex for this. Here's a sample:

    //here $sData is incoming plain text data
    $rgResult = [];
    preg_match_all('/server([^\s]+)\s+([^
    ]+)(((?!server).)*)/msi', $sData, $rgServers, PREG_SET_ORDER);
    foreach($rgServers as $rgMatches)
    {
       $rgResult[$rgMatches[1]][] = [
          'date' => $rgMatches[2],
          'list' => array_map(function($sItem)
          {
             return preg_split('/\s+/', $sItem, -1, PREG_SPLIT_NO_EMPTY);
          }, preg_split('/[
    ]+/', $rgMatches[3], -1, PREG_SPLIT_NO_EMPTY))
       ];
    }
    //var_dump($rgResult);
    

    I've seen your edit with preferred format, but it's unclear (since somehow your servers are one in another in your sample). Example above will separate servers into array and put each date entry also separately with list of users (assuming user name is also coming first).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大