PHP脚本解析地址?
我正在寻找一个 PHP 脚本来解析名称和地址字段,即人们附加到电子邮件末尾的那种字段。它将以纯文本形式上传,并采用以下格式:
John Q. Public
Director of Some Thing
123 Elm Street
Anytown, ST 11001
(000) 555-1212
(000) 555-1213 FAX
地址的格式往往有所不同,这就是为什么我想知道是否有人已经接受了这个。谢谢。
I am looking for a PHP script to parse through a name and address field, the kind that people attach to the end of their emails. It would be uploaded as plain text and would be in this format:
John Q. Public
Director of Some Thing
123 Elm Street
Anytown, ST 11001
(000) 555-1212
(000) 555-1213 FAX
The addresses tend to vary in format, which is why I am wondering if anyone has taken this on yet. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于地址变化很大,因此这不是一项简单的解析任务。如果可能,我建议使用经过验证的服务,例如Google Geocoding API< /a>.
您可以使用 PHP 的 file_get_contents() 来检索答案并 json_decode() 来读取它(JSON 是其中之一格式地理编码 API 可以返回)。
Because addresses can vary so much, this is not a trivial parsing task. If possible, I would recommend using a proven service such as The Google Geocoding API.
You could use PHP's file_get_contents() to retrieve the answer and json_decode() to read it (JSON is one of several formats the Geocoding API can return).
SmartyStreets 提供了一个提取工具来完成此类操作。您可以粘贴任何文本块,它会刮擦以提取其中可能存在的任何地址。 SmartyStreets 专注于地址验证,因此我们的开发人员已经解决了解析问题。您可以在此处查看。该提取器是一个 Web 工具,但您可以使用 PHP 以编程方式使用它。它确实需要至少免费订阅。
下面是从上面的虚假电子邮件签名中发现的内容:
(需要明确的是,我在 SmartyStreets 工作。)
SmartyStreets offers an extraction tool to do this sort of thing. You can paste in any block of text and it will scrape through to extract any addresses that might be in it. SmartyStreets specializes in address validation, so our developers have already sorted out the parsing problem. You can check it out here. The extractor is a web tool, but you can use it programmatically using PHP. It does require at least a free subscription.
Here's a look at what it found from your fake email signature above:
(Just to be clear, I do work at SmartyStreets.)
Google 地理编码每天的限制为 2,500 个。
如果您要解析大量数据,我建议使用其他服务。
可以返回类似于 Google 的格式化地址的最便宜的地理编码 API 是 地理编码农场。他们也有 json,所以你仍然可以将 file_get_contents 与 json_decode() 一起使用。如果您有大量数据,我建议您使用他们的 25,000/天(25 美元/月)并通过它进行解析。如果超过这个数量,请升级到其他计划。如果您只需要 2,500 但将来需要更多,我建议您仍然使用它们,因为它们也是免费的 2,500,并且您可以在其仪表板中立即将计划升级为付费计划。
下站点:
他们不像谷歌那样提供地址组件,只提供返回/更正的地址...:/希望他们将来添加地址组件。我发现唯一缺少的东西。 :(
PS 要么谷歌他们,要么我将他们的网站链接到上面的“地理编码农场”这个词...
PSS 抱歉回复晚了,但希望这会帮助其他寻找这个的人...
Google Geocoding has a limit of 2,500 per day.
If you have alot of data being parsed, I suggest using another services.
The cheapest geocoding api that can return a formatted address similar to Google would be Geocode Farm. They have jSON also, so you can still use file_get_contents with json_decode(). I would suggest you use their 25,000/day ($25/mo) and parse it out through that if you have alot of data. If more than that, upgrade to a different plan. If you only need 2,500 but more in the future, I suggest you still use them because they are free for 2,500 too and you can upgrade the plan instantly to paid one in their dashboard.
The down site:
They don't provide the address components as Google does, only the returned / corrected address... :/ Hopefully they add address components in the future. The only thing I've found that's missing. :(
P.S. Either Google them or I linked their site to the word "Geocode Farm" above...
P.S.S. Sorry for late reply, but hopefully this will help someone else who looks for this...