preg_match() problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

preg_match() problems

Post 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:

Code: Select all

Array (
  &#1111;0] =&gt; -RA
)
I need it to return:

Code: Select all

Array (
  &#1111;0] =&gt; -RA
  &#1111;1] =&gt; 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. :/
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 - :wink:

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
(
    &#1111;0] =&gt; Array
        (
            &#1111;0] =&gt; AUTO
            &#1111;1] =&gt; KT
            &#1111;2] =&gt; SM
            &#1111;3] =&gt; -RA
            &#1111;4] =&gt; BR
            &#1111;5] =&gt; OV
            &#1111;6] =&gt; RM
            &#1111;7] =&gt; AO
            &#1111;8] =&gt; RA
        )

)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Ooops there was a mistake in my regex :oops:

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
(
    &#1111;0] =&gt; Array
        (
            &#1111;0] =&gt; AUTO
            &#1111;1] =&gt; -RA
            &#1111;2] =&gt; BR
        )

)
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

shoowee, thanks! I've tried all sorts of things, including:

Code: Select all

/ \-?\+?&#1111;A-Z]{2} (?=.*&#1111;A-Z]{3}&#1111;\d]{3} ) | &#1111;A-Z]{2} (?=.*&#1111;A-Z]{3}&#1111;\d]{3} )/
just for the 2 digit one. XD Bah, I gotta get better at these things. :P

Thanks. ;)

Edit: Actually needed an addition to it. I'm working with a big-ass METAR to bugfix things:

Code: Select all

/&#1111;\-\+]?\b&#1111;A-Z]{2}(?:&#1111;A-Z]{2})?\b(?=.*&#1111;A-Z]{3}&#1111;\d]{3})/
AUTO isn't actually a part of it, but I'll just take that out with an if statement. ^^;
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
(
    &#1111;0] =&gt; Array
        (
            &#1111;0] =&gt; AUTO 32013G19KT 1 3/4SM -RA
            &#1111;1] =&gt; BR
        )

    &#1111;1] =&gt; Array
        (
            &#1111;0] =&gt; -RA
            &#1111;1] =&gt; BR
        )

)
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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) :P
Post Reply