Parse A Steet Address into components

删除回忆录丶 提交于 2019-11-28 04:33:24

问题


Does 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


回答1:


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




回答2:


Edit: 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 Regexes. I have always found this cheat sheet very useful:

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




回答3:


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.




回答4:


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.




回答5:


How about this one,

http://www.analysisandsolutions.com/software/addr/




回答6:


I've found a php address parser that was designed for Poland but might work elsewhere with some modification:

PHP address parser




回答7:


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 LiveAddress which is an address verification web service API.



来源:https://stackoverflow.com/questions/1739746/parse-a-steet-address-into-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!