PHP:将邮政编码分成两部分?

发布于 2025-01-02 16:23:49 字数 454 浏览 0 评论 0原文

我需要将英国邮政编码一分为二。我有一些代码可以获取前半部分,但它并没有涵盖所有内容(例如 gir0aa)。有没有人有更好的方法可以验证所有英国邮政编码然后将其分为前半部分和后半部分?谢谢。

function firstHalf($postcode) {
if(preg_match('/^(([A-PR-UW-Z]{1}[A-IK-Y]?)([0-9]?[A-HJKS-UW]?[ABEHMNPRVWXY]?|[0-9]?[0-9]?))\s?([0-9]{1}[ABD-HJLNP-UW-Z]{2})$/i',$postcode))
    return preg_replace('/^([A-Z]([A-Z]?\d(\d|[A-Z])?|\d[A-Z]?))\s*?(\d[A-Z][A-Z])$/i', '$1', $postcode);
}

将 ig62ts 拆分为 ig6 或将 cm201ln 拆分为 cm20。

I need to split a UK postcode into two. I have some code that gets the first half but it doesn't cover everything (such as gir0aa). Does anyone have anything better that validates all UK postcodes then breaks it into the first and second half? Thanks.

function firstHalf($postcode) {
if(preg_match('/^(([A-PR-UW-Z]{1}[A-IK-Y]?)([0-9]?[A-HJKS-UW]?[ABEHMNPRVWXY]?|[0-9]?[0-9]?))\s?([0-9]{1}[ABD-HJLNP-UW-Z]{2})$/i',$postcode))
    return preg_replace('/^([A-Z]([A-Z]?\d(\d|[A-Z])?|\d[A-Z]?))\s*?(\d[A-Z][A-Z])$/i', '$1', $postcode);
}

will split ig62ts into ig6 or cm201ln into cm20.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

找回味觉 2025-01-09 16:23:49

incode 始终是一个数字后跟两个字母字符,因此最简单的拆分方法是砍掉最后三个字符,以便轻松验证它。

修剪所有空格:它们纯粹是为了便于人类阅读而使用的。

剩下的第一部分是输出代码。这可以是单个字母字符,后跟 1 或 2 位数字;两个字母字符后跟 1 或 2 位数字;或者一两个字符后跟一个数字,后跟一个附加的字母字符。

有几个值得注意的例外: SAN TA1 是公认的邮政编码,GIR 0AA 也是如此;但这是唯一两个不遵循标准模式的。

要测试邮政编码是否有效,正则表达式并不够......您需要进行查找来检索该信息。

The incode is always a single digit followed by two alpha characters, so the easiest way to split is to chop off the last three characters, allowing it to be validated easily.

Trim any spaces: they're used purely for ease of human readability.

The first part that remains is then the outcode. This can be a single alpha character followed by 1 or 2 digits; two alpha characters followed by 1 or 2 digits; or one or two characters followed by a single digit, followed by an additional alpha character.

There are a couple of notable exceptions: SAN TA1 is a recognised postcode, as is GIR 0AA; but these are the only two that don't follow the standard pattern.

To test if a postcode is valid, a regexp isn't really adequate... you need to do a lookup to retrieve that information.

維他命╮ 2025-01-09 16:23:49

如果您不关心验证,请根据此处的信息(页面底部有不同的正则表达式,包括您的)http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom 您可以用于除安圭拉以外的任何地方

  $str = "BX3 2BB";
  preg_match('#^(.*)(\s+)?(\d\w{2})$#', $str, $matches);
  echo "Part #1 = " . $matches[1];
  echo "<br>Part #2 = " . $matches[3];

If you do not care about validation, based on information here (at the bottom of the page there are different regexps, including yours) http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom your can use for everything except of Anguilla

  $str = "BX3 2BB";
  preg_match('#^(.*)(\s+)?(\d\w{2})$#', $str, $matches);
  echo "Part #1 = " . $matches[1];
  echo "<br>Part #2 = " . $matches[3];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文