0

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,

2
  • I'm not sure what's wrong with your code. I just copy and pasted what you provided and ran it locally. No problems and it only returned the records with METAR SA. Commented Oct 16, 2014 at 19:34
  • Yeah, but its returning the records with Array ( [0] => in the front, and the new program doesn't allow that. Commented Oct 16, 2014 at 19:47

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried it and thr result is Array () anyways the program doesn't allow the Array ( [0] => at the beginning
My bad, I was missing a ->nodeValue inside the condition. As for the beginning Array(), the print_r function will do that, you shouldn't use it. No need for it anymore since I added an echo straight from the loop.
Thanks, any idea about how I could do the same without print_r()?
Yes, see my updated code. I use an echo straight from the loop and added an end-of-line character.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.