Asha Posted April 23, 2007 Share Posted April 23, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/48270-solved-very-very-basic-php-question/ Share on other sites More sharing options...
MadTechie Posted April 23, 2007 Share Posted April 23, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/48270-solved-very-very-basic-php-question/#findComment-235995 Share on other sites More sharing options...
Asha Posted April 23, 2007 Author Share Posted April 23, 2007 Unfortunately the weather's changed! So I can't test it yet :\ But it hasn't broken anything, apparently, and it looks like it might work? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/48270-solved-very-very-basic-php-question/#findComment-236074 Share on other sites More sharing options...
paul2463 Posted April 23, 2007 Share Posted April 23, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/48270-solved-very-very-basic-php-question/#findComment-236209 Share on other sites More sharing options...
MadTechie Posted April 23, 2007 Share Posted April 23, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/48270-solved-very-very-basic-php-question/#findComment-236225 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.