PHP PCRE - 从地址获取城市

发布于 2024-10-07 12:25:24 字数 337 浏览 0 评论 0原文

我需要从完整地址获取邮政编码后面包含的最后一个单词(邮政编码应该始终保持相同的顺序)。

地址示例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗酒趁年少 2024-10-14 12:25:24

像这样的事情应该可以做到。

preg_match('/\d{4}-\d{3}\s+(.*)/', $subject, $match);
echo $match[1];

PS:我不会尝试使用 reg exp 解析它,因为它效率不高,如果用户将城市放在邮政编码之前怎么办?如果您 100% 确定您的数据,那就没问题。

Something like this should be able to do it.

preg_match('/\d{4}-\d{3}\s+(.*)/', $subject, $match);
echo $match[1];

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.

燃情 2024-10-14 12:25:24

这将获取邮政编码(4 位数字、破折号、3 位数字)和空格之后字符串的最后部分。

preg_match('/\s+\d{4}-\d{3}\s+(.+)$/', $address, $match);
print_r($match);

This will get the last part of the string after the postal code (4 digits, dash, 3 digits) and whitespace.

preg_match('/\s+\d{4}-\d{3}\s+(.+)$/', $address, $match);
print_r($match);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文