验证英国短(向外)邮政编码 [英] Validate UK short (outward) postcode

查看:16
本文介绍了验证英国短(向外)邮政编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此验证器:

//validate postcode
function IsPostcode($postcode)
{
    $postcode = strtoupper(str_replace(' ','',$postcode));
    if(preg_match("/^[A-Z]{1,2}[0-9]{2,3}[A-Z]{2}$/",$postcode) || preg_match("/^[A-Z]{1,2}[0-9]{1}[A-Z]{1}[0-9]{1}[A-Z]{2}$/",$postcode) || preg_match("/^GIR0[A-Z]{2}$/",$postcode))
    {
        return true;
    }
    else
    {
        return false;
    }
}

From this link.

但我希望能够验证像ME20和TN10这样的邮政编码,而不是全面的ME20 4RN,TN0 4RN。这是邮政编码的一部分,称为"外发代码"。

有人能帮我解决正则表达式吗?

推荐答案

您可以使用我更新的正则表达式来解决您的问题

从我这一端开始验证英国邮政编码

<?php 
function IsPostcode($postcode)
{
    $postcode = strtoupper(str_replace(' ','',$postcode));
    if(preg_match("/(^[A-Z]{1,2}[0-9R][0-9A-Z]?[s]?[0-9][ABD-HJLNP-UW-Z]{2}$)/i",$postcode) || preg_match("/(^[A-Z]{1,2}[0-9R][0-9A-Z]$)/i",$postcode))
    {    
        return true;
    }
    else
    {
        return false;
    }
}

echo $result = IsPostcode('ME20');
?>

输出

%1

希望这一定会对您有所帮助。

这篇关于验证英国短(向外)邮政编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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