Page 1 of 1
preg_match() problems
Posted: Fri Apr 08, 2005 4:13 pm
by Skara
I'm writing a script to read weather metars, but I'm having a problem finding the precipitation ones. They are always either 2 or 4 cap'd letters sometimes preceded by a + or -.
Examples: RA, BR, +TSRA, -DZ...
Anyway, how can I get preg_match() to return all occurances?
The following is a small portion of my code:
Code: Select all
<?php
$metar = "072345Z AUTO 32013G19KT 1 3/4SM -RA BR OVC006 09/09 A2982 RMK AO2 RAE19B41 P0000";
preg_match("/[-?+?][[A-Z]{2}|[A-Z]{4}]/", $metar, $precip);
print_r($precip);
?>
But it only returns:
I need it to return:
Code: Select all
Array (
ї0] => -RA
ї1] => BR
)
I need it to keep finding the occurances until there aren't any more. So how can I do that? I'm sure it's something fairly simple, but I couldn't find anything on php.net or google. :/
Posted: Fri Apr 08, 2005 5:33 pm
by Chris Corbyn
Not 100% if this is what you mean but it'll take all 4 letter or 2 letter UPPERCASE words possibly preceded by a + or -
If it needs tweaking for something else gimme a shout.
Code: Select all
<?php
$metar = "072345Z AUTO 32013G19KT 1 3/4SM -RA BR OVC006 09/09 A2982 RMK AO2 RAE19B41 P0000";
//preg_match("/[-?+?][[A-Z]{2}|[A-Z]{4}]/", $metar, $precip);
preg_match_all('/[\-\+]?[A-Z]{2}(?:[A-Z]{2})?/', $metar, $precip);
print_r($precip);
?>
Code: Select all
Array
(
ї0] => Array
(
ї0] => AUTO
ї1] => KT
ї2] => SM
ї3] => -RA
ї4] => BR
ї5] => OV
ї6] => RM
ї7] => AO
ї8] => RA
)
)
Posted: Fri Apr 08, 2005 6:20 pm
by Chris Corbyn
Ooops there was a mistake in my regex
Missing a \b (so it doesn't match partial word)
Corrected
Code: Select all
<?php
$metar = "072345Z AUTO 32013G19KT 1 3/4SM -RA BR OVC006 09/09 A2982 RMK AO2 RAE19B41 P0000";
//preg_match("/[-?+?][[A-Z]{2}|[A-Z]{4}]/", $metar, $precip);
preg_match_all('/[\-\+]?\b[A-Z]{2}(?:[A-Z]{2})?\b/', $metar, $precip);
print_r($precip);
?>
Code: Select all
Array
(
ї0] => Array
(
ї0] => AUTO
ї1] => -RA
ї2] => BR
)
)
Posted: Fri Apr 08, 2005 6:37 pm
by Skara
shoowee, thanks! I've tried all sorts of things, including:
Code: Select all
/ \-?\+?їA-Z]{2} (?=.*їA-Z]{3}ї\d]{3} ) | їA-Z]{2} (?=.*їA-Z]{3}ї\d]{3} )/
just for the 2 digit one. XD Bah, I gotta get better at these things.
Thanks.
Edit: Actually needed an addition to it. I'm working with a big-ass METAR to bugfix things:
Code: Select all
/ї\-\+]?\bїA-Z]{2}(?:їA-Z]{2})?\b(?=.*їA-Z]{3}ї\d]{3})/
AUTO isn't actually a part of it, but I'll just take that out with an if statement. ^^;
Posted: Fri Apr 08, 2005 6:55 pm
by Chris Corbyn
Skara wrote:AUTO isn't actually a part of it, but I'll just take that out with an if statement. ^^;
Does AUTO always come before everything else?
Code: Select all
<?php
$metar = "072345Z AUTO 32013G19KT 1 3/4SM -RA BR OVC006 09/09 A2982 RMK AO2 RAE19B41 P0000";
//preg_match("/[-?+?][[A-Z]{2}|[A-Z]{4}]/", $metar, $precip);
preg_match_all('/(?:AUTO.*?)?([\-\+]?\b[A-Z]{2}(?:[A-Z]{2})?\b(?=.*[A-Z]{3}[\d]{3}))/', $metar, $precip);
echo '<pre>';
print_r($precip);
?>
You'll have to read the first index now since I've added the parens to exclude AUTO. Just disregard the zeroth index (thats just ALL of what it sees).
Code: Select all
Array
(
ї0] => Array
(
ї0] => AUTO 32013G19KT 1 3/4SM -RA
ї1] => BR
)
ї1] => Array
(
ї0] => -RA
ї1] => BR
)
)
Posted: Sat Apr 09, 2005 2:12 pm
by Skara
nah, AUTO isn't always there. AUTO is only there when the report is automated. Most of the time an actual person has to send it.

(which is really neat to think that just a few blocks away at my small town airport there's a guy that send that like 5 minutes ago)
