Last active
March 8, 2022 18:20
-
-
Save guimadaleno/9360012 to your computer and use it in GitHub Desktop.
Função simples para validar CPF (autor desconhecido)
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
<?php | |
function is_cpf_valid ($cpf) | |
{ | |
$cpf = preg_replace("/[^0-9]/", "", $cpf); | |
$digitoUm = 0; | |
$digitoDois = 0; | |
for($i = 0, $x = 10; $i <= 8; $i++, $x--): | |
$digitoUm += $cpf[$i] * $x; | |
endfor; | |
for($i = 0, $x = 11; $i <= 9; $i++, $x--): | |
if (str_repeat($i, 11) == $cpf): | |
return false; | |
endif; | |
$digitoDois += $cpf[$i] * $x; | |
endfor; | |
$calculoUm = (($digitoUm%11) < 2) ? 0 : 11-($digitoUm%11); | |
$calculoDois = (($digitoDois%11) < 2) ? 0 : 11-($digitoDois%11); | |
if ($calculoUm <> $cpf[9] || $calculoDois <> $cpf[10]): | |
return false; | |
else: | |
return true; | |
endif; | |
} | |
# Usage | |
if (is_cpf_valid("01234567890")) | |
echo "ok!"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment