Jump to content

Recommended Posts

Hi - I was wondering if someone could tell me how to express the following in PHP:

 

If it has "NSC" anywhere in the string "$metar"

 

- display 'clear skies' on one line

 

- take the block of letters after NSC (should be something like "13/10") and take the part before the "/"

 

-- if it has a leading zero (e.g. 06) then remove it (i.e. 6)

 

-- if it has M in the number (e.g. M10) change it to - (i.e. -10).

 

-- then display it as "avg. temperature 13C" (etc.) in a new line.

 

- take the last block of letters (should be something like "Q1019") and take the number after "Q", then display it as "pressure 1019 millibars" in another line

 

(An example $metar would be METAR = EGSC 230850Z 22008KT 150V260 8000 NSC 13/10 Q1019)

 

Um, I'm actually hoping to tack it to the end of this script (it seemed to me the question itself is independent of the script, so put it in this forum instead of 3rd Party Scripts. Sorry if I'm wrong!)

 

In any case, any help would be really appreciated. Thank you so much in advance!

Link to comment
https://forums.phpfreaks.com/topic/48270-solved-very-very-basic-php-question/
Share on other sites

so i guess something like

<?php
$subject = "METAR = EGSC 230850Z 22008KT 150V260 8000 NSC 13/10 Q1019";

if (preg_match('/NSC (\\w+)\/.*Q(\\w+)/', $subject, $regs)) {
$temp = preg_replace("/[Mm]/","-",$regs[1]."C");
$display = "avg. temperature $temp<br />pressure {$regs[2]} millibars";
} else {
$display = "nothing found";
}

echo $display;
?> 

 

 

results

avg. temperature 13C

pressure 1019 millibars

 

or for

METAR = EGSC 230850Z 22008KT 150V260 8000 NSC M13/10 Q1019

avg. temperature -13C

pressure 1019 millibars

MadTechie

 

i think there may be a bit of misunderstanding in you post, not all METAR's will have NSC in them ( stands for Nil Significant Cloud if you were interested), in the original post it states that if NSC is there then to print out "Clear Skies" on one line before the average temperature etc your code does wonderfully.

Ahh messed up their

i read it as

If it has "NSC" anywhere in the string "$metar" then do blar blar ..

thanxs for point that out paul2463

 

 

Ok revised code

<?php

#$subject = "METAR = EGSC 230850Z 22008KT 150V260 8000 NSC 13/10 Q1019"; //test1
#$subject = "METAR = EGSC 230850Z 22008KT 150V260 8000 NSC M13/10 Q1019"; //test2
#$subject = "METAR = EGSC 230850Z 22008KT 150V260 8000 13/10 Q1019"; //test3
$subject = "METAR = EGSC 230850Z 22008KT 150V260 8000 03/10 Q1019"; //test4

if (strpos($subject, 'NSC'))
{
$display = "Clear Skies<br />";
}
if (preg_match('/(\\w+)\/.*Q(\\w+)/', $subject, $regs)) {
$temp = preg_replace("/[Mm]/","-",(int)$regs[1]."C");
$display .= "avg. temperature $temp<br />pressure {$regs[2]} millibars";
} else {
$display = "nothing found";
}

echo $display;

?> 

 

of couse i am using the / to find them but without more example this is the best i can do..

 

as a note i could just use a word delimitor but this works just as well

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.