I need some help converting an old ereg expression to preg_match. I'm wanting to pull the barometer (altimeter) out of a airport METAR.
ereg('^(A|Q)([0-9]{4})',$part,$pieces)
The above is the expression I need to convert to preg_match. I tried this the following but it returns '0'.
preg_match('/^(A)([0-9]{4})/',$metar)
For those who are unfamiliar with a METAR, it looks like this: KLAX 230353Z 26007KT 9SM BKN009 19/16 A2985 RMK AO2 SLP105 T01890161 The altimeter is the A2985.
You must remove the begining anchor ^
, since your substring is not at the begining of the string.
If you want to extract something you must add a result variable:
if (preg_match('/([AQ])([0-9]{4})/',$metar, $match))
print_r($match);
Note that for more security, you could add word boundaries:
if (preg_match('/\b([AQ])([0-9]{4})\b/',$metar, $match))
print_r($match);