简体   繁体   English

正则表达式PHP:电话号码国际格式,+

[英]Regex PHP: Phone number international format, +

Users should enter a phone number in international format, it should always start with a +, and without any special chars, so for example +3162503277. 用户应输入国际格式的电话号码,该电话号码应始终以+开头,并且不得包含任何特殊字符,例如+3162503277。

I thought this simple regex would do it, but somehow I must be missing something, because it doesn't accept the phone number: 我以为这个简单的正则表达式可以做到,但是由于某种原因,我不见了,因为它不接受电话号码:

if (!preg_match('/^\+?[0-9]{6}$/i', $phone)) 
    fail("Invalid phone number");

What should be the way to check if the number starts with a + and further only contains numbers? 如何检查数字是否以+开头并且仅进一步包含数字?

Appending ? 追加? after \\+ makes + optional. \\+使+可选之后。

[0-9]{6}$ match only 6 digits. [0-9]{6}$仅匹配6位数字。 To match more digits, you have to use + isntead of {6} . 要匹配更多数字,您必须使用{6} + nottead。 If you want 6 or more digits, use {6,} . 如果需要6位或更多位数,请使用{6,}

Try following: 请尝试以下操作:

if (!preg_match('/^\+\d+$/', $phone)) 
    fail("Invalid phone number");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM