src/Validation.php
public static function cpf($cpf)
    {
        // Extract numbers
        $cpf = preg_replace('/[^\d]/', '', $cpf);

        // Check amount of numbers
        if (strlen($cpf) > 11) {
            return false;
        }
        $cpf = str_pad($cpf, 11, '0', STR_PAD_LEFT);

        // Check for same digit sequence
        if (preg_match('/(\d)\1{10}/', $cpf)) {
            return false;
        }

        // Calculate check digits
        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 $cpf;
    }
public static function cpf($cpf)
    {
        // Extract numbers
        $cpf = preg_replace('/[^\d]/', '', $cpf);

        // Check amount of numbers
        if (strlen($cpf) > 11) {
            return false;
        }
        $cpf = str_pad($cpf, 11, '0', STR_PAD_LEFT);

        // Check for same digit sequence
        if (preg_match('/(\d)\1{10}/', $cpf)) {
            return false;
        }

        // Calculate check digits
        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 $cpf;
    }
public static function cpf ( $ cpf ) { // Extract numbers $ cpf = preg_replace ( '/[^\d]/' , '' , $ cpf ) ; // Check amount of numbers if ( strlen ( $ cpf ) > 11 ) { return false ; } $ cpf = str_pad ( $ cpf , 11 , '0' , STR_PAD_LEFT ) ; // Check for same digit sequence if ( preg_match ( '/(\d)\1{10}/' , $ cpf ) ) { return false ; } // Calculate check digits 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 $ cpf ; }
Validate Brazilian CPF

@author rafael-neri (modified)
@link https://gist.github.com/rafael-neri/ab3e58803a08cb4def059fce4e3c0e40

@param string $cpf Up to 11 digits, anything else is discarded

@return string validated (only numbers) or false if invalid
Validate Brazilian CPF