Tag Archive for function

Parsing a United States Location String with PHP

So I needed a way to parse/extract a location string into the city and state or zip code for a client’s site. If you don’t quite know what I’m talking about, head over to Superpages and look at the location field. A couple Google searches later, I decided to just go ahead an write my own function. This sounds easy until you think about it. Any of the following inputs need to be able to be parsed.

  1. dallas tx
  2. dallas, texas
  3. los angeles california
  4. washington disctrict of columbia
  5. richmond virginia
  6. charleston west virginia
  7. 90001

This is beyond the scope of a simple regular expression. We can’t simply use the last word of the input either since the state could be multiple words like in case #4. There’s no way to determine which word or group of words is the state without testing the string against an array of states. As you can see, cases like #5 and #6 complicate things even further because if we test from the end of the string, virginia could also be west virginia

Here is the function I came up with. Please let me know if you find a bug or condition that cannot be parsed. Read the rest of this entry »