Skip to content

Instantly share code, notes, and snippets.

@dmandrade
Last active April 29, 2020 15:27
Show Gist options
  • Save dmandrade/794caadeea13fae472959feb0fc20beb to your computer and use it in GitHub Desktop.
Save dmandrade/794caadeea13fae472959feb0fc20beb to your computer and use it in GitHub Desktop.
validar cpf
<?php
function validaCpf($cpf) {
$cpf = preg_replace("/\D+/", "", $cpf);
if(strlen($cpf) < 11 || count(array_count_values(str_split($cpf))) === 1) {
return false;
}
for ($t = 9; $t < 11; $t++) {
for ($dv = 0, $c = 0; $c < $t; $c++) {
$dv += $cpf[$c] * (($t + 1) - $c);
}
$dv = ((10 * $dv) % 11) % 10;
if ($cpf[$c] != $dv) {
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment