PHP PCRE - 从地址获取城市
我需要从完整地址获取邮政编码后面包含的最后一个单词(邮政编码应该始终保持相同的顺序)。
地址示例:
Av. Elias Garcia (em frente ao Restaurante Pote) Palhais 2815-233 Charneca da Caparica
我需要获取这部分:
Charneca da Caparica
如您所见,这是邮政编码后面的最后一个单词:
2815-233
我需要一个 PCRE 表达式来匹配此部分。有什么建议吗?
I need to get from a full address the last words it contains, after the postal code (which should always be in the same order).
An example of an address:
Av. Elias Garcia (em frente ao Restaurante Pote) Palhais 2815-233 Charneca da Caparica
I need to get this part:
Charneca da Caparica
which, as you can see, is the last words after the postal code:
2815-233
I need a PCRE expression to match this. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样的事情应该可以做到。
PS:我不会尝试使用 reg exp 解析它,因为它效率不高,如果用户将城市放在邮政编码之前怎么办?如果您 100% 确定您的数据,那就没问题。
Something like this should be able to do it.
PS: I wouldn't try to parse this using reg exp because it won't be efficient, what if the user put the city before the zip code. If you are 100% sure of your data it should be fine.
这将获取邮政编码(4 位数字、破折号、3 位数字)和空格之后字符串的最后部分。
This will get the last part of the string after the postal code (4 digits, dash, 3 digits) and whitespace.