Parse A Steet Address in Components

Does anyone have a php class or regex for parsing an address in components? At the very least, it should break down into these components: street information, condition, zip code, country

+4
source share
7 answers

The library / language agnostic solution is designed to use Google geocoding for this. It can return detailed, broken information about a specific address.

http://code.google.com/apis/maps/documentation/services.html#Geocoding_Structured

+7
source

Edit: use this as an example if your data is formatted very accurately. As Strager noted, in most cases there will be too many changes to the data to use the regular expression effectively.

Assuming your input is in the format:

[Street Name], [State], [ZIP], [Country] 

This regex will do the trick:

 m/^(.+?),(.+?),([0-9]+),(.+)$/ 

But regular expressions are quite complicated ... if you are going to use this for something significant, I would suggest spending time learning Regexes. I always found this trickster very useful:

http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/

+3
source

If you are talking about pre-existing data, good luck. If this is what you control input over, I recommend creating a separation of the different parts of the address at the input level. Yus's suggestion.

+2
source

The following is a Python version using pyparsing to parse street addresses. This is not PHP, but may give you some idea of ​​the complexity of the problem.

+1
source

The problem is that the addresses themselves come in all shapes and sizes, and they are not self-identifying objects. This means that there is no way to find out if you did this correctly without checking the address manually (and even then it may be error prone) or using some kind of address verification software - whether it be desktop software or online -Appendix.

There are several web address verification services that can take an address and break it down into its component parts, and do it in a safe way when the results have been certified as valid.

I must mention that I am the founder of SmartyStreets. We are conducting a review that includes the features that you specified for addresses in the United States. Our flagship product is LiveAddress, which is an address verification web service API .

0
source

I found a php address parser that was developed for Poland, but can work elsewhere with some modification:

php parser

0
source

All Articles