开发者

Regular expression for getting string part

I have some string like

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

from these i need "VIC" . Can anyone help me to find out a solution using regex or php string function

Regular expression for getting string part

开发者_Python百科

This is the actual strings... that read from an excel sheet i want the states name from these string like, NSW, ACT, VIC etc


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


if it's always next-to-last, just split the string on space and take the penultimate element.


To get vic with its value:

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

To get just VIC:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜