Skip to content

Instantly share code, notes, and snippets.

@henriqueboaventura
Created July 4, 2012 19:12
Show Gist options
  • Save henriqueboaventura/3049018 to your computer and use it in GitHub Desktop.
Save henriqueboaventura/3049018 to your computer and use it in GitHub Desktop.
cpf validation
function validaCPF($cpf) {
$cpf = str_pad(ereg_replace('[^0-9]', '', $cpf), 11, '0', STR_PAD_LEFT);
if (strlen($cpf) != 11 || $cpf == '00000000000' || $cpf == '11111111111' || $cpf == '22222222222' || $cpf == '33333333333' || $cpf == '44444444444' || $cp
return false;
} else {
for ($t = 9; $t < 11; $t++) {
for ($d = 0, $c = 0; $c < $t; $c++) {
$d += $cpf{$c} * (($t + 1) - $c);
}
$d = ((10 * $d) % 11) % 10;
if ($cpf{$c} != $d) {
return false;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment