php 电话号码的 preg_match 表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20477639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:14:02  来源:igfitidea点击:

preg_match expression for phone numbers

phpregexpreg-matchphone-number

提问by user3083952

I want to display an error message if the number doesn't match any number with hyphen, plus sign, space or brackets. No numbers either.

如果数字与任何带有连字符、加号、空格或括号的数字都不匹配,我想显示一条错误消息。也没有数字。

For example:

例如:

(012) 123 4567
(012)-123-4567
012-345-6789
123 123 1234
+12 23 213 3456

The above examples all work with this expression:

上面的例子都适用于这个表达式:

if (!preg_match("/^[0-9\-]|[\+0-9]|[0-9\s]|[0-9()]*$/", $_POST['tel'])) {
    $telErr = "Invalid contact number";
}

But it allows letters, which I do not want.

但它允许字母,这是我不想要的。

Example:

例子:

+00000000a

The above example is accepted by the expression I have.

上面的例子被我的表达所接受。

Please can someone help me with this.

请有人帮我解决这个问题。

回答by SimsSidy

This expression'll accept +5number, 5number

这个表达式将接受 +5number, 5number

"'^(([\+]([\d]{2,}))([0-9\.\-\/\s]{5,})|([0-9\.\-\/\s]{5,}))*$'"

回答by webMan

I first cleanup the string a bit (that avoids useless matches):

我首先清理一下字符串(避免无用的匹配):

$input = preg_replace('/[^0-9+\(\)-]/', '', $_POST['tel']);

I than match an american number +1 (xxx) xxx-xxxx;

我比匹配一个美国号码 +1 (xxx) xxx-xxxx;

if(preg_match('/^(\+1|001)?\(?([0-9]{3})\)?([ .-]?)([0-9]{3})([ .-]?)([0-9]{4})/',$input))
        $result = "match: USA";

else    $result = "no match";

this allows for quite some input configurations - only your last number would not match (if I'd not do the cleanup first, not because of the international code which is matched, but because of the split in the area-code) all the others go also without cleanup.

这允许相当多的输入配置 - 只有您的最后一个数字不匹配(如果我不先进行清理,不是因为匹配的国际代码,而是因为区域代码中的拆分)所有其他去也没有清理。

回答by Tomás Gaete

It seems to me that what you want to express is in the ballpark of:

在我看来,你想要表达的大概是:

\+{0,1}\({0,1}[0-9]{0,3}\){0,1}[ -]{0,1}[0-9]{0,4}[ -]{0,1}[0-9]{0,4}[ -]{0,1}[0-9]{0,4}

that can be reduced to:

可以简化为:

(\+?\(?[0-9]{2,3}\)?)([ -]?[0-9]{2,4}){3}

This can be read this way:

可以这样读:

\+?                  //matches existence of +
\(?                  //matches existence of (
[0-9]{2,3}           //matches 2 to 3 numbers
\)?                  //matches existence of )
([ -]?[0-9]{2,4}){3} //matches a space or a dash with 2 to 4 numbers, 3 times.

This will give you a maximum of 3 + 4 * 3 = 15 numbers without spaces for the phone variable.

这将为您提供最多 3 + 4 * 3 = 15 个数字,电话变量没有空格。

But the best way in my opinion is to trim the input and then count it's length.

但我认为最好的方法是修剪输入,然后计算它的长度。

In RegEx there's always a perfect matching answer, but it doesn't always means it's a good idea to use it. It can be too complicated to maintain or too hard to understand.

在 RegEx 中总是有一个完美匹配的答案,但并不总是意味着使用它是个好主意。维护起来可能太复杂或太难理解。

回答by japetko

Better regular expression is:

更好的正则表达式是:

"/^[\+0-9\-\(\)\s]*$/"