Skip to content

Instantly share code, notes, and snippets.

@fernandovaller
Last active September 4, 2021 20:49
Show Gist options
  • Save fernandovaller/57a73f8cdf66da877d95fa7bbd08225d to your computer and use it in GitHub Desktop.
Save fernandovaller/57a73f8cdf66da877d95fa7bbd08225d to your computer and use it in GitHub Desktop.
PHP – Validar CPF e CNPJ em PHP
<?php
// Validar numero de cpf
function validar_cpf($cpf) {
// Verificar se foi informado
if(empty($cpf))
return false;
// Remover caracteres especias
$cpf = preg_replace('/[^0-9]/', '', $cpf);
// Verifica se o numero de digitos informados
if (strlen($cpf) != 11)
return false;
// Verifica se todos os digitos são iguais
if (preg_match('/(\d)\1{10}/', $cpf))
return false;
// Calcula os digitos verificadores para verificar se o
// CPF é válido
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;
}
// Validar numero de CNPJ
function validar_cnpj($cnpj) {
// Verificar se foi informado
if(empty($cnpj))
return false;
// Remover caracteres especias
$cnpj = preg_replace('/[^0-9]/', '', $cnpj);
// Verifica se o numero de digitos informados
if (strlen($cnpj) != 14)
return false;
// Verifica se todos os digitos são iguais
if (preg_match('/(\d)\1{13}/', $cnpj))
return false;
$b = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
for ($i = 0, $n = 0; $i < 12; $n += $cnpj[$i] * $b[++$i]);
if ($cnpj[12] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
return false;
}
for ($i = 0, $n = 0; $i <= 12; $n += $cnpj[$i] * $b[$i++]);
if ($cnpj[13] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment