<?php
// Правильные:
$correctNumbers = [
'84951234567', '+74951234567', '8-495-1-234-567',
' 8 (8122) 56-56-56', ' 8 (81222) 56-6-56', '8-911-1234567', '8 (911) 12 345 67',
'8-911 12 345 67', '8 (911) - 123 - 45 - 67', '+ 7 999 123 4567',
'8 ( 999 ) 1234567', '8 999 123 4567'
];
// Неправильные:
$incorrectNumbers = [
'+7-(812)-567-55-67', '+7(-812)1234567', '+7(812-)1234567', '+7-(812)1234567', '+781(((2123)))))4567',
'02', '84951234567 позвать люсю', '849512345', '849512345678',
'8 (409) 123-123-123', '7900123467', '5005005001', '8888-8888-88',
'84951a234567', '8495123456a',
'+1 234 5678901', /* неверный код страны */
'+8 234 5678901', /* либо 8 либо +7 */
'7 234 5678901' /* нет + */
];
function checkPhoneNumber($phone) {
return preg_match('#^(?:8|\+7)(\()?(?(1)|-?)\d(?:-?\d){2}(?(1)(?(?!\d-?\d?\))(\))))(?:-?\d)(?(1)(?(2)|(?(?!\d\))(\)))))(?:-?\d)(?(1)(?(2)|(?(3)|\))))(?:-?\d){5}$#', str_replace(' ', '', $phone));
}
function checkPhoneNumberIRL($phone) {
}
foreach (array_merge($correctNumbers, $incorrectNumbers) as $phoneNumber) { echo checkPhoneNumber($phoneNumber) ? $phoneNumber . " верный сын Аллаха\n" : $phoneNumber . " неверный кафир\n";
}
echo "\n\nКак будет в настоящем проекте:\n\n";
foreach (array_merge($correctNumbers, $incorrectNumbers) as $phoneNumber) { echo checkPhoneNumberIRL($phoneNumber) ? $phoneNumber . " верный сын Аллаха\n" : $phoneNumber . " неверный кафир\n";
}
PD9waHAKCi8vINCf0YDQsNCy0LjQu9GM0L3Ri9C1OiAKJGNvcnJlY3ROdW1iZXJzID0gWyAKICAnODQ5NTEyMzQ1NjcnLCAgJys3NDk1MTIzNDU2NycsICc4LTQ5NS0xLTIzNC01NjcnLCAKICAnIDggKDgxMjIpIDU2LTU2LTU2JywgJyA4ICg4MTIyMikgNTYtNi01NicsICc4LTkxMS0xMjM0NT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