抓取和分解数据 [英] Grab and Explode Data
问题描述
我对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屋!