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

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

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

        // Remove check digits
        $value = substr($cnpj, 0, -2);

        // Calculate check digits
        $cd = 11 - self::mod11($value);
        if ($cd >= 10) {
            $cd = 0;
        }
        $value .= $cd;
        $cd = 11 - self::mod11($value);
        if ($cd >= 10) {
            $cd = 0;
        }
        $value .= $cd;

        // Verify
        if ($cnpj === $value) {
            return $cnpj;
        }
        return false;
    }
public static function cnpj($cnpj)
    {
        // Extract numbers
        $cnpj = preg_replace('/[^\d]/', '', $cnpj);

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

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

        // Remove check digits
        $value = substr($cnpj, 0, -2);

        // Calculate check digits
        $cd = 11 - self::mod11($value);
        if ($cd >= 10) {
            $cd = 0;
        }
        $value .= $cd;
        $cd = 11 - self::mod11($value);
        if ($cd >= 10) {
            $cd = 0;
        }
        $value .= $cd;

        // Verify
        if ($cnpj === $value) {
            return $cnpj;
        }
        return false;
    }
public static function cnpj ( $ cnpj ) { // Extract numbers $ cnpj = preg_replace ( '/[^\d]/' , '' , $ cnpj ) ; // Check amount of numbers if ( strlen ( $ cnpj ) > 14 ) { return false ; } $ cnpj = str_pad ( $ cnpj , 14 , '0' , STR_PAD_LEFT ) ; // Check for same digit sequence if ( preg_match ( '/(\d)\1{13}/' , $ cnpj ) ) { return false ; } // Remove check digits $ value = substr ( $ cnpj , 0 , - 2 ) ; // Calculate check digits $ cd = 11 - self :: mod11 ( $ value ) ; if ( $ cd >= 10 ) { $ cd = 0 ; } $ value .= $ cd ; $ cd = 11 - self :: mod11 ( $ value ) ; if ( $ cd >= 10 ) { $ cd = 0 ; } $ value .= $ cd ; // Verify if ( $ cnpj === $ value ) { return $ cnpj ; } return false ; }
Validates Brazilian CNPJ

@param string $cnpj Up to 14 digits, anything else is discarded

@return string validated (only numbers)
@return false  if invalid
Validates Brazilian CNPJ