【发布时间】:2013-07-31 09:37:08
【问题描述】:
我正在使用这个验证器:
//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;
}
}
但我希望能够验证 ME20 和 TN10 等邮政编码,而不是完整的 ME20 4RN、TN0 4RN。这是邮政编码的一部分,称为“外向编码”。
有人可以帮我解决正则表达式吗?
【问题讨论】:
-
有一个库可用于执行此操作gist.github.com/ChrisMcKee/4452116 只需将 incode 设置为可选以仅验证第一部分
-
您对正则表达式模式了解多少?只需要最基本的正则表达式知识就可以从您已经获得的起点计算出您的答案。我建议去reading up a bit more about regex,而不是问这样一个基本问题。这样你会学到更多。
-
我真的对它们一无所知,但我只是买了“掌握正则表达式”
标签: php regex preg-match postal-code