I'm new to php and I'm trying to write a function to find an invalid postcode. This is an option, however I've been told this isnt the ideal format:
我是php的新手,我正在尝试编写一个函数来查找无效的邮政编码。这是一个选项,但我被告知这不是理想的格式:
function postcode_valid($postcode) {
return preg_match('/\w{2,3} \d\w{2}/', $postcode);
}
//more accurate
//[A-Z]{1,2}[0-9]{1,2}[A-Z]? [0-9][A-Z]{2}
I understand the first function, but I don't know how to write the 'ideal' solution as a function, please can you advise?
我理解第一个功能,但我不知道如何编写'理想'解决方案作为一个功能,请你指教?
1 个解决方案
#1
0
If the regular expression you provided in the comment field is the correct one and you don't know how to use it in PHP, here is the solution:
如果您在注释字段中提供的正则表达式是正确的,并且您不知道如何在PHP中使用它,那么这是解决方案:
function postcode_valid($postcode) {
return preg_match('/^[A-Z]{1,2}[0-9]{1,2}[A-Z]? [0-9][A-Z]{2}$/', $postcode);
}
You need to add two slashes (one in front, one at the end) of the regular expression and pack it in a string in PHP. I would also highly recommend you to use ^
and $
at the beginning resp. at the end of the regular expression to indicate the beginning and the end of the string (otherwise, it is valid, if only a part of the string contains the correct pattern i.e. a longer string with a valid part would be accepted.) Here is a live example.
您需要在正则表达式中添加两个斜杠(一个在前面,一个在末尾),并将其打包在PHP中的字符串中。我还强烈建议你在开头使用^和$。在正则表达式的末尾指示字符串的开头和结尾(否则,它是有效的,如果只有字符串的一部分包含正确的模式,即将接受具有有效部分的较长字符串。)这是一个现实的例子。
If you are looking for the validation of a UK post code, you should be using the following regex instead (source):
如果您正在寻找英国邮政编码的验证,您应该使用以下正则表达式(来源):
(GIR 0AA)|((([A-Z-[QVX]][0-9][0-9]?)|(([A-Z-[QVX]][A-Z-[IJZ]][0-9][0-9]?)|(([A-Z-[QVX]][0-9][A-HJKPSTUW])|([A-Z-[QVX]][A-Z-[IJZ]][0-9][ABEHMNPRVWXY])))) [0-9][A-Z-[CIKMOV]]{2})
If you are looking for something else, please provide a comment below.
如果您正在寻找其他内容,请在下面提供评论。
#1
0
If the regular expression you provided in the comment field is the correct one and you don't know how to use it in PHP, here is the solution:
如果您在注释字段中提供的正则表达式是正确的,并且您不知道如何在PHP中使用它,那么这是解决方案:
function postcode_valid($postcode) {
return preg_match('/^[A-Z]{1,2}[0-9]{1,2}[A-Z]? [0-9][A-Z]{2}$/', $postcode);
}
You need to add two slashes (one in front, one at the end) of the regular expression and pack it in a string in PHP. I would also highly recommend you to use ^
and $
at the beginning resp. at the end of the regular expression to indicate the beginning and the end of the string (otherwise, it is valid, if only a part of the string contains the correct pattern i.e. a longer string with a valid part would be accepted.) Here is a live example.
您需要在正则表达式中添加两个斜杠(一个在前面,一个在末尾),并将其打包在PHP中的字符串中。我还强烈建议你在开头使用^和$。在正则表达式的末尾指示字符串的开头和结尾(否则,它是有效的,如果只有字符串的一部分包含正确的模式,即将接受具有有效部分的较长字符串。)这是一个现实的例子。
If you are looking for the validation of a UK post code, you should be using the following regex instead (source):
如果您正在寻找英国邮政编码的验证,您应该使用以下正则表达式(来源):
(GIR 0AA)|((([A-Z-[QVX]][0-9][0-9]?)|(([A-Z-[QVX]][A-Z-[IJZ]][0-9][0-9]?)|(([A-Z-[QVX]][0-9][A-HJKPSTUW])|([A-Z-[QVX]][A-Z-[IJZ]][0-9][ABEHMNPRVWXY])))) [0-9][A-Z-[CIKMOV]]{2})
If you are looking for something else, please provide a comment below.
如果您正在寻找其他内容,请在下面提供评论。