PHP 电话正则表达式 - 不要超过 8 个数字的长度



我正在尝试创建一个电话正则表达式。

$regexNumber = '#^[1-9][0-9]{8}$#';

现在它说第一个数字应该以1-9开头,而接下来的数字应该在0-9之间。

当我想控制电话号码不超过8个号码的长度时,问题就出现了。我以为花括号可以解决我的问题,但我可能用错了。我希望读过我的帖子的人能给我一些启发。

提前谢谢你。

使用

$regexNumber = '#^(?!0)d{8}$#';

简单明了:8位数字,但开头没有0

参见正则表达式证明。

--------------------------------------------------------------------------------
^                        the beginning of the string
--------------------------------------------------------------------------------
(?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
0                        '0'
--------------------------------------------------------------------------------
)                        end of look-ahead
--------------------------------------------------------------------------------
d{8}                    digits (0-9) (8 times)
--------------------------------------------------------------------------------
$                        before an optional n, and the end of the
string

最新更新