php获取邮政编码的第一部分 [英] php get the first part of postcode

查看:129
本文介绍了php获取邮政编码的第一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用数据库提取搜索计算的邮政编码的第一部分。有任何想法吗?!我需要这个在网站中实现一个搜索模块,它根据邮政编码寻找一些相关信息。但我的邮政编码数据库,它没有第二部分的邮政编码,只有第一部分是有。它的英国邮政编码。

解决方案

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

  / ** 
* @param string $ postcode英国邮政编码
* @return string
* /
function getUKPostcodeFirstPart($ postcode)
{
//验证输入参数
$ postcode = strtoupper($ postcode);

//英国大陆/海峡群岛(简化版本,因为我们不需要验证它)
if(preg_match('/ ^ [AZ]([AZ]?\d (\ d | [AZ])?| \d [AZ]?)\s *?\d [AZ] [AZ] $ / i',$ postcode))
return preg_replace / ^([AZ]([AZ]?\d(\d | [AZ])?| \d [AZ]?))\s *?(\d [AZ] [AZ]) $ / i','$ 1',$ postcode);
//英语强制
if(preg_match('/ ^(BFPO)\s *?(\d {1,4})$ / i',$ postcode))
return preg_replace('/ ^(BFPO)\s *?(\d {1,4})$ / i','$ 1',$ postcode);
//海外领地
if(preg_match('/ ^(ASCN | BBND | BIQQ | FIQQ | PCRN | SIQQ | STHL | TDCU | TKCA)\s *?(1ZZ)$ / i' ,$ postcode))
return preg_replace('/ ^([AZ] {4})\s *?(1ZZ)$ / i','$ 1',$ postcode)

//好...甚至其他形式的邮政编码...返回它是
return $ postcode;
}

测试数据

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

结果

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

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

编辑:oops,它不包括GIR 0AA,对不起


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?!

解决方案

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

这篇关于php获取邮政编码的第一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆