抓取和分解数据 [英] Grab and Explode Data

查看:130
本文介绍了抓取和分解数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PHP有点新鲜感,并且需要一些关于从文件中爆炸数据的帮助。有问题的文件是: http://data.vattastic.com/vatsim-data.txt!CLIENTS:部分(靠近底部)的数据。有了这些数据,我需要分解它并获取每个之间的信息。



我试过这段代码,但它给了我一个可变的偏移量错误( Undefined offset:3

  $ file = file(http://data.vattastic.com/vatsim-data.txt); 
foreach($ file as $ line)
{
$ data_record = explode(:,$ line);

//只抓取ATC中的数据...
if($ data_record [3] =='ATC'&& $ data_record [16]! ='1'&& $ data_record [18]!='0'&& stristr($ data_record [0],'OBS')=== FALSE)
{
这里的代码...
}
}

如果有人可以帮我这个,我非常感激。

解决方案

发生这种情况是因为你试图像这样爆炸行:


; !GENERAL包含常规设置

当你分解这行时,你的 $ data_records
$ b


数组(
[0] =>;!GENERAL包含常规设置)


快速解决方案:

  $ file = file http://data.vattastic.com/vatsim-data.txt); 
foreach($ file as $ line)
{
if(strpos($ line,';')=== 0)continue; //这是评论。忽略
$ data_record = explode(:,$ line);
$ col_count = count($ data_record);

switch($ col_count){
case 42:// columns qty = 42,所以这是来自`clients`的行
//只抓取具有ATC ...
if($ data_record [3] =='ATC'&& $ data_record [16]!='1'&& $ amp; $ data_record [18]!='0' && stristr($ data_record [0],'OBS')=== FALSE)
{
其余代码在这里...
}
break;
默认值:
//这是其他类型的数据,忽略
break;
}
}


I'm sort of new to PHP, and I need some help on exploding data from a file. The file in question is: http://data.vattastic.com/vatsim-data.txt

Basically, I need to get the data under the !CLIENTS: section (near the bottom). With this data, I need to explode it and get the info between each :.

I have tried with this code, but it gives me a variable offset error (Undefined offset: 3)

$file = file("http://data.vattastic.com/vatsim-data.txt");
foreach($file as $line)
{
   $data_record = explode(":", $line);

   // grab only the data that has "ATC" in it...
   if($data_record[3] == 'ATC' && $data_record[16] != '1' && $data_record[18] != '0'&&   stristr($data_record[0],'OBS') === FALSE)
   {
        rest of code here...
   }
}

If someone could help me with this, I'd greatly appreciate it.

解决方案

This happens because you are trying to explode rows like this:

; !GENERAL contains general settings

When you explode that line, you your $data_records looks like this:

Array ( [0] => ; !GENERAL contains general settings )

Quick solution:

$file = file("http://data.vattastic.com/vatsim-data.txt");
foreach($file as $line)
{
   if(strpos($line,';') === 0) continue ; // this is comment. ignoring
   $data_record = explode(":", $line);
   $col_count = count($data_record);

   switch($col_count) {
     case 42: // columns qty = 42, so this is row from `clients`
      // grab only the data that has "ATC" in it...
      if($data_record[3] == 'ATC' && $data_record[16] != '1' && $data_record[18] != '0'&&   stristr($data_record[0],'OBS') === FALSE)
      {
           rest of code here...
      }
      break;
     default:
       // this is other kind of data, ignoring
       break;
   }
}

这篇关于抓取和分解数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆