<?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 "ok - ";
//приведение к единому виду
$number = preg_replace("/[-() ]/", "", $number); //чистка мусора $number = preg_replace("/^\\+7/", "8", $number); //замена семерки echo $number;
}
else
{
echo "failed";
}
echo "\n";
}
}
echo "These should be ok:\n";
checkNumbers($correctNumbers, $regex);
echo "\nThese should be failed:\n";
checkNumbers($incorrectNumbers, $regex);
PD9waHAKCi8vItGD0YHQu9C+0LbQvdC10L3QvdCw0Y8iINC30LDQtNCw0YfQsCDRgSDQvdC+0LzQtdGA0LDQvNC4CiAKJGNvcnJlY3ROdW1iZXJzID0gWyAKICAnODQ5NTEyMzQ1NjcnLCAgJys3NDk1MTIzNDU2NycsICc4LTQ5NS0xLTIzNC01NjcnLCAKICAnIDggKDgxMjIpIDU2LTU2LTU2JywgJzgtOTExLTEyMzQ1NjcnLCAnOCAoOTExKSAxMiAzNDUgNjcnLCAKICAnOC05MTEgMTIgMzQ1IDY3JywgJzggKDkxMSkgLSAxMjMgLSA0NSAtIDY3JywgJysgNyA5OTkgMTIzIDQ1NjcnLCAKICAnOCAoIDk5OSApIDEyMzQ1NjcnLCAnOCA5OTkgMTIzIDQ1NjcnCl07CiAKJGluY29ycmVjdE51bWJlcnMgPSBbCiAgJ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