如何检测变量中的地址或邮政编码

sha*_*ail 0 php

我的问题听起来可能有所不同,但我真的在寻找解决方案.

我在PHP中有一个变量,比方说$check,包含一个字符串.它可以是地址或邮政编码.我们可以在这里申请检查变量是否有邮政编码或地址吗?

编辑:本网站适用于英国

谢谢

Chr*_*ris 7

详细链接:这里

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;
    }
Run Code Online (Sandbox Code Playgroud)

使用:

$e = "AB235RB";
if (IsPostcode($e))
print "Valid Post Code";
else
print "Invalid Post Code";
Run Code Online (Sandbox Code Playgroud)

我已根据此编写了英国邮政编码验证器,所以我知道它有效:)