Jump to content

\s problem in filtering the city from address


Nuv

Recommended Posts

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

 

 

 

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.