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 Link to comment https://forums.phpfreaks.com/topic/231664-s-problem-in-filtering-the-city-from-address/ 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); Link to comment https://forums.phpfreaks.com/topic/231664-s-problem-in-filtering-the-city-from-address/#findComment-1192083 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 Link to comment https://forums.phpfreaks.com/topic/231664-s-problem-in-filtering-the-city-from-address/#findComment-1192102 Share on other sites More sharing options...
silkfire Posted March 25, 2011 Share Posted March 25, 2011 Anytime, Nuv. Link to comment https://forums.phpfreaks.com/topic/231664-s-problem-in-filtering-the-city-from-address/#findComment-1192105 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.