Regular expression for getting string part

Solution:1

preg_match_all('/(?P<state>[A-Z]{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 a regex in multiline mode to match the 2 or 3 capital letters preceding a whitespace character and a 4 digit postcode.

I called it state because I live in Australia and these are all Australian states 🙂

Solution:2

To get vic with its value:

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

To get just VIC:

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