国际格式电话号码的Regex


Regex for phone number in international format

我需要写一个正则表达式,只匹配这种格式的

+420 000 000 000
+420000000000
420 000 000 000
420000000000

它不能匹配字符串任何部分的任何a-z字符,只能匹配数字、空格和开头的"+"。

您可以尝试以下regex,

^'+?'d{3} ?'d{3} ?'d{3} ?'d{3}$

演示

  • ^断言我们处于起点
  • '+?可选+
  • 'd{3}正好匹配三位数字
  • <space>?可选空间
  • $断言我们已经到了终点

此正则表达式应该适用于您:

$mobileNumber = "0905 222 222";
 if ( preg_match("/^('+?)([0-9] ?){9,20}$/", $mobileNumber) )
            echo "matches!";