Jump to content

\s problem in filtering the city from address


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.