We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
开发者_运维百科 Improve this questionDoes anyone have a php class, or regex to parse an address into components? At least, it should break up into these components: street info, state, zip, country
A library/language agnostic solution would be to use Google's geocoder for this. It can return detailed, broken-down information about a given address.
http://code.google.com/apis/maps/documentation/services.html#Geocoding_Structured
Use this just as an example if your data is all formatted very similarly. As Strager pointed out, in most cases there will be too much variation in data to use a regex effectively.
Assuming your input is of the format:
[Street Name], [State], [ZIP], [Country]
This regex will do the trick:
m/^(.+?),(.+?),([0-9]+),(.+)$/
But regular expressions are fairly complex. If you are going to use this for anything significant, I would suggest taking the time to learn them. I have always found the "Regular Expressions Cheat Sheet" very useful.
If you're talking about pre-existing data, good luck to ye. If this is something that you have control over the input for, I recommend creating separation of the different parts of the address at the input level. Jus' a suggestion.
Here is a Python version using pyparsing for parsing street addresses. It's not PHP, but might give you some insights into the complexity of the problem.
The issue is that addresses themselves come in all shapes and sizes and they are not self-validating entities. This means that there is no way to really know if you did it right without inspecting the address by hand (and even then it can be error prone) or by using some kind of address verification software--be it desktop-based software or online.
There are a number of address verification web services that can take an address and break it into its component parts and do so in a safe manner where the results have been certified to be valid.
I should mention that I'm the founder of SmartyStreets. We do address verification which includes the capabilities that you have asked about for US-based addresses. Our flagship product is US Address API which is an address verification web service API.
How about this one,
http://www.analysisandsolutions.com/software/addr/
I've found a php address parser that was designed for Poland but might work elsewhere with some modification:
PHP address parser
精彩评论