Nuv Posted March 25, 2011 Share Posted March 25, 2011 I would like to get the city from the address below.It seems that it has ,(comma) in the end.Thus i used regex. However the result isn't as i expected. \s is a whitespace. Then why am i getting "East 31st Street Oakland" and not just Oakland ? Also how should the regex be changed so that i get Oakland ? Address 1411 East 31st Street Oakland, CA 94602-1018 Regex i am using preg_match("~\s(.*?),~", $row[1], $fetchcity) ; print_r($fetchcity); result Array ( [0] => East 31st Street Oakland, [1] => East 31st Street Oakland ) 1 Quote Link to comment Share on other sites More sharing options...
silkfire Posted March 25, 2011 Share Posted March 25, 2011 Your regex starts matching after the first space it finds, which in this case is after "1411". The logic here instead is to start matching all non-space characters until you collide with a comma. preg_match('#([^ ]+),#', $row[1], $fetchcity); Quote Link to comment Share on other sites More sharing options...
Nuv Posted March 25, 2011 Author Share Posted March 25, 2011 Your regex starts matching after the first space it finds, which in this case is after "1411". The logic here instead is to start matching all non-space characters until you collide with a comma. preg_match('#([^ ]+),#', $row[1], $fetchcity); Your logic works flawlessly. Thankyou Quote Link to comment Share on other sites More sharing options...
silkfire Posted March 25, 2011 Share Posted March 25, 2011 Anytime, Nuv. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.