我是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);
// 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...
}
}
如果有人可以帮助我,我会非常感激。
答案 0 :(得分:2)
这是因为你试图像这样爆炸行:
!GENERAL包含常规设置
当你爆炸那条线时,你的$data_records
看起来像这样:
阵列( [0] => ; !GENERAL包含常规设置)
快速解决方案:
$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;
}
}
答案 1 :(得分:0)
另一种解决方案是使用正则表达式并查找!CLIENTS:
部分。这也适用于CLIENTS将来有多于或少于42列
$file = file_get_contents ("http://data.vattastic.com/vatsim-data.txt");
$matches = null;
preg_match ('/!CLIENTS:\s\n(.*)\n;/s' , $file, $matches );
if($matches)
{
$client_lines = explode("\n", $matches[1]);
foreach ($client_lines as $client)
{
$data_record = explode(":", $client);
if($data_record[3] == 'ATC' && $data_record[16] != '1' && $data_record[18] != '0'&& stristr($data_record[0],'OBS') === FALSE)
{
//rest of code here...
}
}
}