Regular expression to get the string part

I have a line like

Shp 1,737 Plenty Rd, Mill Park VIC 3082 560 High St, Preston VIC 3072 217 Mickleham Rd, Tullamarine VIC 3043 

from this I need "VIC". Can someone help me find a solution using regex function or php string enter image description here

These are the actual lines that are read on the excel sheet, I want the name of the states from these lines, for example NSW, ACT, VIC , etc.

+4
source share
3 answers
 preg_match_all('/(?P<state>[AZ]{2,3})\s\d{4}$/m', $str, $matches); var_dump($matches['state']); 

Output

 array(3) { [0]=> string(3) "VIC" [1]=> string(3) "VIC" [2]=> string(3) "VIC" } 

This uses regex in multi-line mode according to two or three uppercase letters preceding the space character and 4-digit postal code.

I called it state because I live in Australia, and that's all Australian states :)

+3
source

if it is always near the last, just divide the line into a space and take the penultimate element.

+3
source

To get the vic value with its value:

 $getvic = substr($myString,-8,-7); 

To get only VIC:

 $getvic = substr($myString,-8,-2); 
0
source

All Articles