php 获取邮政编码的第一部分

发布于 2024-11-14 22:09:51 字数 127 浏览 5 评论 0原文

我需要使用数据库提取邮政编码的第一部分以进行搜索计算。有什么想法吗?我需要它在网站中实现一个搜索模块,该模块根据邮政编码查找一些相关信息。但是我拥有的邮政编码数据库没有邮政编码的第二部分,只有第一部分。它是英国邮政编码。那么,有什么想法吗?

I need to extract the first part of postcode for search calculation using a database. Any ideas?! I need this to implement a search module in the site, which looks out for some relevant information according to the post code. But the database which I am having for post code, it doesnt have second part of the postcode, only the first part is there. Its for UK postcode. So, any ideas?!

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

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

发布评论

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

评论(1

一个人的旅程 2024-11-21 22:09:51

这不会覆盖 100% 的所有可能的英国邮政编码,但可以覆盖 99.999% 左右:)

/**
 * @param string $postcode UK Postcode
 * @return string
 */
function getUKPostcodeFirstPart($postcode)
{
    // validate input parameters
    $postcode = strtoupper($postcode);

    // UK mainland / Channel Islands (simplified version, since we do not require to validate it)
    if (preg_match('/^[A-Z]([A-Z]?\d(\d|[A-Z])?|\d[A-Z]?)\s*?\d[A-Z][A-Z]$/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);
    // British Forces
    if (preg_match('/^(BFPO)\s*?(\d{1,4})$/i', $postcode))
        return preg_replace('/^(BFPO)\s*?(\d{1,4})$/i', '$1', $postcode);
    // overseas territories
    if (preg_match('/^(ASCN|BBND|BIQQ|FIQQ|PCRN|SIQQ|STHL|TDCU|TKCA)\s*?(1ZZ)$/i', $postcode))
        return preg_replace('/^([A-Z]{4})\s*?(1ZZ)$/i', '$1', $postcode);

    // well ... even other form of postcode... return it as is
    return $postcode;
}

测试数据:

echo 'TW135BQ -> ', getUKPostcodeFirstPart('TW135BQ'), "\n";
echo 'gir0aa -> ', getUKPostcodeFirstPart('gir0aa'), "\n";
echo 'RG5 3SQ -> ', getUKPostcodeFirstPart('RG5 3SQ'), "\n";
echo 'AB51 0GQ -> ', getUKPostcodeFirstPart('AB51 0GQ'), "\n";
echo 'YO104EA -> ', getUKPostcodeFirstPart('YO104EA'), "\n";
echo 'SE154NS -> ', getUKPostcodeFirstPart('SE154NS'), "\n";
echo 'W1B4BD -> ', getUKPostcodeFirstPart('W1B4BD'), "\n";

结果:

TW135BQ -> TW13
gir0aa -> GIR0AA
RG5 3SQ -> RG5
AB51 0GQ -> AB51
YO104EA -> YO10
SE154NS -> SE15
W1B4BD -> W1B

请注意: 您有责任提供有效的邮政编码。

编辑:哎呀,它不包括 GIR 0AA,抱歉

This will not cover 100% of all possible UK postcodes, but will do for like 99.999% or so :)

/**
 * @param string $postcode UK Postcode
 * @return string
 */
function getUKPostcodeFirstPart($postcode)
{
    // validate input parameters
    $postcode = strtoupper($postcode);

    // UK mainland / Channel Islands (simplified version, since we do not require to validate it)
    if (preg_match('/^[A-Z]([A-Z]?\d(\d|[A-Z])?|\d[A-Z]?)\s*?\d[A-Z][A-Z]$/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);
    // British Forces
    if (preg_match('/^(BFPO)\s*?(\d{1,4})$/i', $postcode))
        return preg_replace('/^(BFPO)\s*?(\d{1,4})$/i', '$1', $postcode);
    // overseas territories
    if (preg_match('/^(ASCN|BBND|BIQQ|FIQQ|PCRN|SIQQ|STHL|TDCU|TKCA)\s*?(1ZZ)$/i', $postcode))
        return preg_replace('/^([A-Z]{4})\s*?(1ZZ)$/i', '$1', $postcode);

    // well ... even other form of postcode... return it as is
    return $postcode;
}

Test data:

echo 'TW135BQ -> ', getUKPostcodeFirstPart('TW135BQ'), "\n";
echo 'gir0aa -> ', getUKPostcodeFirstPart('gir0aa'), "\n";
echo 'RG5 3SQ -> ', getUKPostcodeFirstPart('RG5 3SQ'), "\n";
echo 'AB51 0GQ -> ', getUKPostcodeFirstPart('AB51 0GQ'), "\n";
echo 'YO104EA -> ', getUKPostcodeFirstPart('YO104EA'), "\n";
echo 'SE154NS -> ', getUKPostcodeFirstPart('SE154NS'), "\n";
echo 'W1B4BD -> ', getUKPostcodeFirstPart('W1B4BD'), "\n";

Results:

TW135BQ -> TW13
gir0aa -> GIR0AA
RG5 3SQ -> RG5
AB51 0GQ -> AB51
YO104EA -> YO10
SE154NS -> SE15
W1B4BD -> W1B

Please note: It is your responsibility to provide valid postcode.

EDIT: oops, it does not cover GIR 0AA, sorry

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文