2017-02-20 66 views
0

我不明白为什么我写的代码无法识别电话号码。 下面是代码:不明白为什么我的PHP程序无法检查电话号码?

<?php 
     $sPattern = "/^ 
      (?:         # Area Code 
       (?:        
        \(       # Open Parentheses 
        (?=\d{3}\))     # Lookahead. Only if we have 3 digits and a closing parentheses 
       )? 
       (\d{3})       # 3 Digit area code 
       (?: 
        (?<=\(\d{3})    # Closing Parentheses. Lookbehind. 
        \)       # Only if we have an open parentheses and 3 digits 
       )? 
       [\s.\/-]?      # Optional Space Delimeter 
      )? 
      (\d{3})        # 3 Digits 
      [\s\.\/-]?       # Optional Space Delimeter 
      (\d{4})\s?       # 4 Digits and an Optional following Space 
      (?:         # Extension 
       (?:        # Lets look for some variation of 'extension' 
        (?: 
         (?:e|x|ex|ext)\.?  # First, abbreviations, with an optional following period 
        | 
         extension    # Now just the whole word 
        ) 
        \s?       # Optionsal Following Space 
       ) 
       (?=\d+)       # This is the Lookahead. Only accept that previous section IF it's followed by some digits. 
       (\d+)       # Now grab the actual digits (the lookahead doesn't grab them) 
      )?         # The Extension is Optional 
      $/x";        // /x modifier allows the expanded and commented regex 

     $aNumbers = array(
      'here is your website: 123-456-7890x123', 
      '123.456.7890x123', 
      '123 456 7890 x123', 
      '(123) 456-7890 x123', 
      '123.456.7890x.123', 
      '123.456.7890 ext. 123', 
      '123.456.7890 extension 123456', 
      '123 456 7890', 
      '123-456-7890ex123', 
      '123.456.7890 ex123', 
      '123 456 7890 ext123', 
      '456-7890', 
      '456 7890', 
      '+1 456 7890 x123', 
      '1234567890', 
      '() 456 7890' 
     ); 

     foreach($aNumbers as $sNumber) { 
      if (preg_match($sPattern, $sNumber, $aMatches)) { 
       echo 'Matched ' . $sNumber . "\n"; 
       print_r($aMatches); 
      } else { 
       echo 'Failed ' . $sNumber . "\n"; 
      } 
     } 
    ?> 

这里是输出:

Failed here is your website: 123-456-7890x123 
Matched 123.456.7890x123 Array ([0] => 123.456.7890x123 [1] => 123 [2] => 456 [3] => 7890 [4] => 123) 
Matched 123 456 7890 x123 Array ([0] => 123 456 7890 x123 [1] => 123 [2] => 456 [3] => 7890 [4] => 123) 
Matched (123) 456-7890 x123 Array ([0] => (123) 456-7890 x123 [1] => 123 [2] => 456 [3] => 7890 [4] => 123) 
Matched 123.456.7890x.123 Array ([0] => 123.456.7890x.123 [1] => 123 [2] => 456 [3] => 7890 [4] => 123) 
Matched 123.456.7890 ext. 123 Array ([0] => 123.456.7890 ext. 123 [1] => 123 [2] => 456 [3] => 7890 [4] => 123) 
Matched 123.456.7890 extension 123456 Array ([0] => 123.456.7890 extension 123456 [1] => 123 [2] => 456 [3] => 7890 [4] => 123456) 
Matched 123 456 7890 Array ([0] => 123 456 7890 [1] => 123 [2] => 456 [3] => 7890) 
Matched 123-456-7890ex123 Array ([0] => 123-456-7890ex123 [1] => 123 [2] => 456 [3] => 7890 [4] => 123) 
Matched 123.456.7890 ex123 Array ([0] => 123.456.7890 ex123 [1] => 123 [2] => 456 [3] => 7890 [4] => 123) 
Matched 123 456 7890 ext123 Array ([0] => 123 456 7890 ext123 [1] => 123 [2] => 456 [3] => 7890 [4] => 123) 
Matched 456-7890 Array ([0] => 456-7890 [1] => [2] => 456 [3] => 7890) 
Matched 456 7890 Array ([0] => 456 7890 [1] => [2] => 456 [3] => 7890) 
Failed +1 456 7890 x123 
Matched 1234567890 Array ([0] => 1234567890 [1] => 123 [2] => 456 [3] => 7890) 
Failed() 456 7890 

我关注的是上线:

'here is your website: 123-456-7890x123', 
'+1 456 7890 x123', 

有号码,但不提取它,并检查它。我该怎么做?

+0

只是一个提示:有一个名为'RegEx Tester'的免费工具可以帮助您检查和验证复杂的正则表达式。 – JustOnUnderMillions

+0

我在这里错过了'[\ s。\/- ]?'反斜杠'[\ s \。\/- ]? ',第一个'可选空间直线距离'行 – JustOnUnderMillions

+0

'^'是字符串/行的开始,'这里是你的网站:'不是数字。你可以在开始处添加'(?:[a-z:] +)?'来允许单词,或者删除锚点。例如https://regex101.com/r/0vsffS/1与https://regex101.com/r/0vsffS/2我也会使用'preg_match_all'和'm'修饰符,而不是迭代。 – chris85

回答

3

您的正则表达式模式以“^”开头,所以数字必须从字符串的开头开始。删除此与它匹配即变化:

$sPattern = "/^ 

$sPattern = "/ 

此外,我建议使用regex101.com检查你的正则表达式。我觉得它非常有帮助。

+0

是否可以从该字符串中提取手机?而不是粘贴完整的字符串? –

+0

抱歉,但没有奏效。它显示这个字符串:匹配的'()456 7890' –

+0

'()456 7890'是匹配的,因为'456 7890'是该行内的有效匹配。 – Gondrup