. * * To disable strict typing, comment out the directive below. */ declare(strict_types=1); function isValid(string $number): bool { $number = str_replace(' ', '', $number); if (mb_strlen($number) < 2) { return FALSE; } if (preg_match("/\D/", $number)) { return FALSE; } // At this point, we know the string only contains digits, so ASCII. $digits = str_split($number); $sum = 0; $len = count($digits); for ($i = $len - 1; $i >= 0; $i--) { $digit = $digits[$i]; if (($len - $i) % 2 == 0) { $digit *= 2; if ($digit > 9) { $digit -= 9; } } $sum += $digit; $sum %= 10; } return ($sum % 10) === 0; }