<?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) {
		
	return preg_match('#^(?:8|\+7)\d{10}$#', str_replace([' ', '-', '(', ')'], '', $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";
}