Я не понимаю, почему код, который я написал, не идентифицирует номер телефона. Вот код:Не понимаю, почему моя программа 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',
Есть номер, но это не извлекая его и проверить Это. Как я могу это сделать?
Просто подсказка: есть бесплатные инструменты с именем 'RegEx Tester', которые помогут вам проверить и проверить сложные регулярные выражения. – JustOnUnderMillions
И я ничего не пропустил здесь '[\ s. \/-]?' Обратная косая черта '[\ s \. \/-]? ', первая строка« Необязательный пробел » – JustOnUnderMillions
'^'- начало строки/строки,' вот ваш сайт: 'не является числом. Вы можете добавить '(?: [A-z:] +)?' В начало, чтобы разрешить слова, или удалить привязку. например https://regex101.com/r/0vsffS/1 vs. https://regex101.com/r/0vsffS/2 Я бы также использовал 'preg_match_all' с модификатором' m', а не итерированием. – chris85