public static function validate($nif)
    {
        $nifCodes = 'TRWAGMYFPDXBNJZSQVHLCKE';

        if (9 !== strlen($nif)) {
            return false;
        }
        $nif = strtoupper(trim($nif));

        $sum = (string) self::getCifSum($nif);
        $n = 10 - substr($sum, -1);

        if (preg_match('/^[0-9]{8}[A-Z]{1}$/', $nif)) {
            // DNIs
            $num = substr($nif, 0, 8);

            return $nif[8] == $nifCodes[$num % 23];
        } elseif (preg_match('/^[XYZ][0-9]{7}[A-Z]{1}$/', $nif)) {
            // NIEs normales
            $tmp = substr($nif, 1, 7);
            $tmp = strtr(substr($nif, 0, 1), 'XYZ', '012').$tmp;

            return $nif[8] == $nifCodes[$tmp % 23];
        } elseif (preg_match('/^[KLM]{1}/', $nif)) {
            // NIFs especiales
            return $nif[8] == chr($n + 64);
        } elseif (preg_match('/^[T]{1}[A-Z0-9]{8}$/', $nif)) {
            // NIE extraño
            return true;
        }

        return false;
    }
public static function validate($nif)
    {
        $nifCodes = 'TRWAGMYFPDXBNJZSQVHLCKE';

        if (9 !== strlen($nif)) {
            return false;
        }
        $nif = strtoupper(trim($nif));

        $sum = (string) self::getCifSum($nif);
        $n = 10 - substr($sum, -1);

        if (preg_match('/^[0-9]{8}[A-Z]{1}$/', $nif)) {
            // DNIs
            $num = substr($nif, 0, 8);

            return $nif[8] == $nifCodes[$num % 23];
        } elseif (preg_match('/^[XYZ][0-9]{7}[A-Z]{1}$/', $nif)) {
            // NIEs normales
            $tmp = substr($nif, 1, 7);
            $tmp = strtr(substr($nif, 0, 1), 'XYZ', '012').$tmp;

            return $nif[8] == $nifCodes[$tmp % 23];
        } elseif (preg_match('/^[KLM]{1}/', $nif)) {
            // NIFs especiales
            return $nif[8] == chr($n + 64);
        } elseif (preg_match('/^[T]{1}[A-Z0-9]{8}$/', $nif)) {
            // NIE extraño
            return true;
        }

        return false;
    }
public static function validate ( $ nif ) { $ nifCodes = 'TRWAGMYFPDXBNJZSQVHLCKE' ; if ( 9 !== strlen ( $ nif ) ) { return false ; } $ nif = strtoupper ( trim ( $ nif ) ) ; $ sum = ( string ) self :: getCifSum ( $ nif ) ; $ n = 10 - substr ( $ sum , - 1 ) ; if ( preg_match ( '/^[0-9]{8}[A-Z]{1}$/' , $ nif ) ) { // DNIs $ num = substr ( $ nif , 0 , 8 ) ; return $ nif [ 8 ] == $ nifCodes [ $ num % 23 ] ; } elseif ( preg_match ( '/^[XYZ][0-9]{7}[A-Z]{1}$/' , $ nif ) ) { // NIEs normales $ tmp = substr ( $ nif , 1 , 7 ) ; $ tmp = strtr ( substr ( $ nif , 0 , 1 ) , 'XYZ' , '012' ) . $ tmp ; return $ nif [ 8 ] == $ nifCodes [ $ tmp % 23 ] ; } elseif ( preg_match ( '/^[KLM]{1}/' , $ nif ) ) { // NIFs especiales return $ nif [ 8 ] == chr ( $ n + 64 ) ; } elseif ( preg_match ( '/^[T]{1}[A-Z0-9]{8}$/' , $ nif ) ) { // NIE extraño return true ; } return false ; }
NIF and DNI validation.

@param string $nif The NIF or NIE

@return bool
NIF and DNI validation .