@ 2022-10-04T00:20:41Z <?php
class CPF
{
// Codigo que valida um CPF informado quanto a validade de seus dígitos verificadores.
/**
*
* Verifica se um valor informado é um CPF válido
* @author Marcos Regis <marcos@marcosregis.com>
* @param string $cpf
* @return bool
*/
public static function validar(string $cpf): bool
{
if (strlen($cpf) != 11) {
return false;
}
$elementos = (array)str_split($cpf);
$elementos[10] = 0; // Reduz uma comparação no calculo de $somaB
$somaA = 0;
$somaB = 0;
foreach ($elementos as $indice => $elemento) {
$multiplicador = count($elementos) - $indice;
$somaA += (int)$elemento * (int)($multiplicador > 2 ? $multiplicador - 1 : 0);
$somaB += (int)$elemento * (int)$multiplicador;
}
$moduloA = (($somaA * 10) % 11) % 10;
$moduloB = (($somaB * 10) % 11) % 10;
return preg_replace('#\d{9}(\d{2})$#', '$1', $cpf) == $moduloA . $moduloB;
}
/**
* Filtra um CPF informado em <var>$cpf</var> removendo mascaras e valores como 111.111.111-11 que são
* falsos positivos
* @param string $cpf
* @return string
*/
public static function filtrar(string $cpf): string
{
$cpf = (string)preg_replace('#\D#', '', $cpf);
return (string)preg_replace('#(\d)\1{10}#', '', $cpf);
}
}
$cpfs = [
'529.982.247-25', // válido, com mascara
'123.456.789-09', // válido, regra do resto 0 ou 1
'834.115.113-89', // válido, com mascara
'50365387223', // válido, sem mascara
'112413460-38', // válido, com apenas parte da máscara
'242.629.250-92', // inválido, com máscara
'88888888888', // inválido, falso positivo sem máscaa=ra
'123.456.789', // inválido, incompleto com máscara
'123456789', // inválido, incompleto sem máscara
'', // inválido, vazio
];
foreach ($cpfs as $cpf) {
$cpfFiltrado = CPF::filtrar($cpf);
echo sprintf('O cpf [%s] é %s!' . PHP_EOL, $cpf, (CPF::validar($cpfFiltrado) ? 'válido' : 'inválido'));
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.4.2 0.010 0.010 18.18 8.4.1 0.023 0.005 19.84 8.3.15 0.011 0.007 18.79 8.3.14 0.012 0.006 16.92 8.3.13 0.004 0.004 18.41 8.3.12 0.009 0.003 19.09 8.3.11 0.004 0.004 16.74 8.3.10 0.013 0.007 24.06 8.3.9 0.000 0.008 26.77 8.3.8 0.006 0.003 16.75 8.3.7 0.011 0.007 17.00 8.3.6 0.015 0.004 18.43 8.3.5 0.011 0.004 20.39 8.3.4 0.006 0.009 20.40 8.3.3 0.006 0.009 19.02 8.3.2 0.000 0.008 24.18 8.3.1 0.008 0.000 24.66 8.3.0 0.012 0.006 26.16 8.2.26 0.017 0.003 18.94 8.2.25 0.006 0.003 18.70 8.2.24 0.003 0.006 19.09 8.2.23 0.000 0.010 20.94 8.2.22 0.004 0.004 37.54 8.2.21 0.016 0.003 26.77 8.2.20 0.010 0.000 18.86 8.2.19 0.006 0.012 16.88 8.2.18 0.007 0.007 25.92 8.2.17 0.007 0.007 22.96 8.2.16 0.011 0.004 22.96 8.2.15 0.008 0.000 25.66 8.2.14 0.004 0.004 24.66 8.2.13 0.006 0.003 26.16 8.2.12 0.007 0.000 20.91 8.2.11 0.007 0.003 20.43 8.2.10 0.009 0.000 20.35 8.1.31 0.004 0.004 18.21 8.1.30 0.009 0.000 16.51 8.1.29 0.003 0.007 30.84 8.1.28 0.012 0.004 25.92 8.1.27 0.004 0.004 24.66 8.1.26 0.003 0.005 26.35 8.1.25 0.005 0.005 28.09 8.1.24 0.010 0.000 18.94 8.1.23 0.010 0.000 18.81 8.0.14 0.010 0.003 19.65 8.0.1 0.006 0.003 17.06 7.4.32 0.011 0.008 17.67
preferences:dark mode live preview
56.54 ms | 403 KiB | 5 Q