duandgfh0506 2014-10-16 19:14
浏览 44
已采纳

从脚本输出中获取文本

everyone, I've been using this code for quite a long time

<?php
$url = 'http://www.smn.gov.ar/mensajes/index.php?observacion=metar&operacion=consultar&87582=on&87641=on&87750=on&87765=on&87222=on&87761=on&87860=on&87395=on&87344=on&87166=on&87904=on&87571=on&87347=on&87803=on&87576=on&87162=on&87532=on&87497=on&87097=on&87046=on&87548=on&87217=on&87506=on&87692=on&87418=on&87574=on&87715=on&87374=on&87289=on&87852=on&87178=on&87896=on&87823=on&87270=on&87155=on&87453=on&87925=on&87934=on&87480=on&87047=on&87553=on&87311=on&87909=on&87436=on&87509=on&87912=on&87623=on&87444=on&87129=on&87371=on&87645=on&87022=on&87127=on&87828=on&87121=on&87938=on&87791=on&87448=on';
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTMLFile($url);
libxml_clear_errors();
$xpath = new DOMXpath($dom);

// search for td's containing METAR
$metars = $xpath->query('//td[contains(text(), "METAR SA")]');
if($metars->length <= 0) {
echo 'no metars found';
exit;
}

$data = array();
foreach($metars as $metar) {
$data[] = $metar->nodeValue;
}

echo '<pre>';
print_r($data);

Well, this was working fine, until the program in charge to read the output was updated and now it needs a clear output.

At the momment I'm getting this: http://ar.ivao.aero/weather/metar.php But the program needs it like this:

SABE 161600Z 02006KT 9999 FEW030 24/18 Q1009 =
SAZA 161600Z 18011KT CAVOK 24/08 Q1010 =
SAZB 161700Z 27012KT CAVOK 21/09 Q1011 = 

I don't thought maybe using another script like a file_get_content() could be useful but again its going to show the infromation I don't want to. I also tried replacing print_r() by var_dump() but its the same

Any ideas? There is anyway to get this informatin in a simple txt file?

Regards,

  • 写回答

1条回答 默认 最新

  • duanqian3953 2014-10-16 19:34
    关注

    You need to filter out some data. Try to find out what's common in the info you need to output. For instance, all the required info from your raw print_r data seems to beging with METAR. So

    echo '<pre>';
    foreach($metars as $metar) {
        if(substr($metar->nodeValue, 0, 5) === "METAR") {
             echo str_replace("METAR ", "", $metar->nodeValue) . PHP_EOL;
        }
    }
    

    That removes any lines like Aeropuerto FORMOSA from the output.

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

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100