Created
September 1, 2016 14:16
-
-
Save phsantiago/6049c0d822907d16754074c5c260c853 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
private 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 | |
else if ($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; | |
} | |
} | |
private function validarCNPJ($cnpj) { | |
$cnpj = preg_replace('/[^0-9]/', '', (string) $cnpj); | |
// Valida tamanho | |
if (strlen($cnpj) != 14) | |
return false; | |
// Valida primeiro dígito verificador | |
for ($i = 0, $j = 5, $soma = 0; $i < 12; $i++) | |
{ | |
$soma += $cnpj{$i} * $j; | |
$j = ($j == 2) ? 9 : $j - 1; | |
} | |
$resto = $soma % 11; | |
if ($cnpj{12} != ($resto < 2 ? 0 : 11 - $resto)) | |
return false; | |
// Valida segundo dígito verificador | |
for ($i = 0, $j = 6, $soma = 0; $i < 13; $i++) | |
{ | |
$soma += $cnpj{$i} * $j; | |
$j = ($j == 2) ? 9 : $j - 1; | |
} | |
$resto = $soma % 11; | |
return $cnpj{13} == ($resto < 2 ? 0 : 11 - $resto); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment