2017-02-20 1 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 \. \/-]를 놓쳤는가? ', 첫 번째'Optional Space Delimeter' 라인 – JustOnUnderMillions

+0

'^'는 문자열/라인의 시작 부분이며'here is your website :'는 숫자가 아닙니다. 시작할 때'(? : [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

관련 문제