cagartner
7/17/2017 - 12:09 PM

Verify if brazillian cpf is valid

Verify if brazillian cpf is valid

<?php
    function validateCPF($cpf)
    {

        $cpf = preg_replace('/[^0-9]/', '', (string) $cpf);

        //social login rule exception
        if ($cpf == 'social') {
            return  true;
        }

        if (strlen($cpf) != 11
            || $cpf == '00000000000'
            || $cpf == '11111111111'
            || $cpf == '22222222222'
            || $cpf == '33333333333'
            || $cpf == '44444444444'
            || $cpf == '55555555555'
            || $cpf == '66666666666'
            || $cpf == '77777777777'
            || $cpf == '88888888888'
            || $cpf == '99999999999'
            || $cpf == '12345678900'
            ) {
            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;
        }

        return false;
    }