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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度