Last active
April 21, 2018 17:09
-
-
Save leonirlopes/3a066ec4b73a14f5ee89d705f695241d to your computer and use it in GitHub Desktop.
Script para Validar CPF em PHP
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 validaCPF($cpf = null) | |
{ | |
// Verifica se um número foi informado | |
if (empty($cpf)) { | |
return false; | |
} | |
// Elimina possivel mascara | |
$cpf = preg_replace('/[^0-9]/', '', $cpf); | |
$cpf = str_pad($cpf, 11, '0', STR_PAD_LEFT); | |
// Verifica se o numero de digitos informados é igual a 11 | |
if (strlen($cpf) != 11) { | |
return false; | |
} | |
// Verifica se nenhuma das sequências invalidas abaixo | |
// foi digitada. Caso afirmativo, retorna falso | |
elseif ($cpf == '00000000000' || | |
$cpf == '11111111111' || | |
$cpf == '22222222222' || | |
$cpf == '33333333333' || | |
$cpf == '44444444444' || | |
$cpf == '55555555555' || | |
$cpf == '66666666666' || | |
$cpf == '77777777777' || | |
$cpf == '88888888888' || | |
$cpf == '99999999999') { | |
return false; | |
// Calcula os digitos verificadores para verificar se o | |
// CPF é válido | |
} 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; | |
} | |
} | |
//echo validaCPF('526.615.630-91'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment