Created
October 9, 2017 20:31
-
-
Save joaohcrangel/e2f27433440434ebbfa7a10deba5582f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isValidCPF($number) | |
{ | |
$number = preg_replace('/[^0-9]/', '', (string) $number); | |
if (strlen($number) != 11) | |
return false; | |
for ($i = 0, $j = 10, $sum = 0; $i < 9; $i++, $j--) | |
$sum += $number{$i} * $j; | |
$rest = $sum % 11; | |
if ($number{9} != ($rest < 2 ? 0 : 11 - $rest)) | |
return false; | |
for ($i = 0, $j = 11, $sum = 0; $i < 10; $i++, $j--) | |
$sum += $number{$i} * $j; | |
$rest = $sum % 11; | |
return ($number{10} == ($rest < 2 ? 0 : 11 - $rest)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// Após a versão 7.4 o uso de chaves ficou obsoleto, veja em: https://wiki.php.net/rfc/deprecate_curly_braces_array_access
function isValidCPF(string $number):bool {
}