<?php
$correctNumbers = [
'84951234567', '+74951234567', '8-495-1-234-567',
' 8 (8122) 56-56-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 = [
'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' /* нет + */
];
$regex = '/^ *(\\+ *7|8)([-() ]*[0-9]){10}$/'; //написанная регулярка
function checkNumbers($numbers, $regex)
{
foreach ($numbers as $number)
{
echo "$number - ";
echo (preg_match($regex, $number) ?
"ok" : "failed"); echo "\n";
}
}
echo "These should be ok:\n";
checkNumbers($correctNumbers, $regex);
echo "\nThese should be failed:\n";
checkNumbers($incorrectNumbers, $regex);
PD9waHAKCiRjb3JyZWN0TnVtYmVycyA9IFsgCiAgJzg0OTUxMjM0NTY3JywgICcrNzQ5NTEyMzQ1NjcnLCAnOC00OTUtMS0yMzQtNTY3JywgCiAgJyA4ICg4MTIyKSA1Ni01Ni01NicsICc4LTkxMS0xMjM0NTY3JywgJzggKDkxMSkgMTIgMzQ1IDY3JywgCiAgJzgtOTExIDEyIDM0NSA2NycsICc4ICg5MTEpIC0gMTIzIC0gNDUgLSA2NycsICcrIDcgOTk5IDEyMyA0NTY3JywgCiAgJzggKCA5OTkgKSAxMjM0NTY3JywgJzggOTk5IDEyMyA0NTY3JwpdOwoKJGluY29ycmVjdE51bWJlcnMgPSBbCiAgJzAyJywgJzg0OTUxMjM0NTY3INC/0L7Qt9Cy0LDRgtGMINC70Y7RgdGOJywgJzg0OTUxMjM0NScsICc4NDk1MTIzNDU2NzgnLCAKICAnOCAoNDA5KSAxMjMtMTIzLTEyMycsICc3OTAwMTIzNDY3JywgJzUwMDUwMDUwMDEnLCAnODg4OC04OD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