Php preg_match代码用于匹配电话号码

时间:2016-08-12 07:23:57

标签: php preg-match

preg_match()无效+(91) 111 11111

代码是:

$number = +91 111 11111;

if (preg_match('/^\s*(?:\+?(\d{1,3}))?[(]\+[)]?([-. (]*(\d{3})[-. )]*)?((\d{3})[-. ]*(\d{2,4})(?:[-.x ]*(\d+))?)\s*$/', $number)) {
   echo "True"; 
}

它正在发挥作用。但是没有在+91 (111) 11111

中工作

1 个答案:

答案 0 :(得分:0)

  

您可能需要重新访问regex或尝试以下内容:

<?php

    if (preg_match('#(^[\+\d][\d]{1,2})?(\()?([\d\s]*)?(\))?([-. (]*(\d{3})[-. )]*)?((\d{3})[-. ]*(\d{2,4})(?:[-.x ]*(\d+))?)\s*$#', $number)) {
        echo "True";
    }

更新:更改模式

<?php

    // MATCHES: "+91 (111) 11111" OR "+91 111 11111" OR "91 (101) 23454" 
    // OR "+(91) (101) 23454" 
    // BUT NOT "1234500000000000000000000" OR "+121(121)38778" 
    $rx     = '#(^[\+\d](\()?[\d]{1,2})*(\))?(\s)(\()?([\d\s]*?)(\))?(\s)([\d\s]*?)$#';
    if (preg_match($rx, $number)) {
        //echo "True";
    }