简体   繁体   English

PHP-自定义电话号码格式的正则表达式

[英]PHP - Regular Expression For Custom Phone Number Format

I need to validate phone number with following rule 我需要按照以下规则验证电话号码

  • Must start with following ISD code +352, +91, +33, +49, +32 (string must start with +) 必须以以下ISD代码开头+ 352,+ 91,+ 33,+ 49,+ 32(字符串必须以+开头)
  • ISD code must be followed by numbers only 0-9 (no spaces, symbols or characters) ISD代码后面必须只有数字0-9(不能有空格,符号或字符)
  • Must be minimum 8 characters and maximum 15 最少8个字符,最多15个字符

I am facing difficulty to construct the correct regular expression, so far I only have preg_match('/^\\+\\d+$/', $value) and this is definitely not working. 我在构造正确的正则表达式时遇到了困难,到目前为止,我只具有preg_match('/^\\+\\d+$/', $value) ,这绝对是行不通的。 Any help in here is appreciated. 在这里的任何帮助表示赞赏。

Thanks. 谢谢。

你可以使用正则表达式

^\+(?:(?:91)|(?:49)|(?:3(?:52)|3|2))\d{8,15}

Must be minimum 8 characters and maximum 15 最少8个字符,最多15个字符

I'm assuming this is total count. 我假设这是总数。

Pattern: 图案:

^\\+(?=\\d{8,15}$)(?:32|33|49|91|352)\\d+$

^               // start of line
\+              // a plus
(?=\d{8,15}$)   // look ahead and assert 8 to 15 digits must match
(?:             // grouped alternation (uncaptured)
32|33|49|91|352 // alternations
)               // end of group
\d+             // 1 or more digits
$               // end of line
  • Flags: g, m 标志:g,m
  • Steps: 30 步骤:30

Demo 演示

Your regular expression should look like this one: 您的正则表达式应如下所示:

^\+(352|91|33|49|32)(\d{8,15})$

This page https://regex101.com/ is very helpful for verify and describing regular expressions. https://regex101.com/的页面对于验证和描述正则表达式非常有用。

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

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