dongzouban9871 2018-08-30 12:10
浏览 53
已采纳

如何在php中的一个数组中获取一个文件的多行

I have log file which looks like below:

--f15a0000-A--
[30/Aug/2018:14:06:33 +0200] W4fdyYHC0Xb8YDuIqk5YQgAAAD0 127.0.0.1 55454 127.0.0.1 80
--f15a0000-B--
GET /FormValidation/page1.php HTTP/1.1
Host: localhost
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: http://localhost/FormValidation/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
tanuser: 00198343

--f15a0000-F--
HTTP/1.1 200 OK
X-Powered-By: PHP/5.6.35
Content-Length: 851
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8

--f15a0000-Z--

--bb410000-A--
[30/Aug/2018:14:06:37 +0200] W4fdzYHC0Xb8YDuIqk5YQwAAAD0 127.0.0.1 55454 127.0.0.1 80
--bb410000-B--
POST /FormValidation/validation.php HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 33
Accept: */*
Origin: http://localhost
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost/FormValidation/page1.php
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
tanuser: 00198343

--bb410000-C--
name1=test&email1=ssn%40gmail.com
--bb410000-F--
HTTP/1.1 200 OK
X-Powered-By: PHP/5.6.35
Content-Length: 17
Keep-Alive: timeout=5, max=98
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8

--bb410000-Z--

I want to build an array containing one block per position.

Ex-ex:

array[0][‘rawdata’] contains the first block from --f15a0000-A-- to --f15a0000-Z--,

array[1][‘rawdata’] contains the second block from --bb410000-A-- to --bb410000-Z--.

Can anyone please help me how to do that?

  • 写回答

2条回答 默认 最新

  • dongsuikai8286 2018-08-30 12:27
    关注

    Read through the file and every time you find a -- in the beginning you either start or stop an array-entry.

    $array = array();
    $fh = fopen($file,"r");
    $started = false;
    while (!feof($fh)) {
      $line = fgets($fh);
      if($started) {
        $temp .= $line;
        if(strpos($line, "--") === 0 && strpos($line, "-Z-") > 0) {
          $started = false;
          $array[] = $temp;
        }
      }
      if(strpos($line, "--") === 0 && strpos($line, "-A-") > 0) {
        $started = true;
        $temp = $line;
      }
    }
    fclose($fh);
    print_r($array);
    

    I think this should do it

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。