Kiwwito

Code Snippets and More

PHP function which checks if a given string was a valid spanish DNI/NIE:

function is_valid_dni_nie($string) {
    if (strlen($string) != 9 ||
        preg_match('/^[XYZ]?([0-9]{7,8})([A-Z])$/i', $string, $matches) !== 1) {
        return false;
    }

    $map = 'TRWAGMYFPDXBNJZSQVHLCKE';

    list(, $number, $letter) = $matches;

    return strtoupper($letter) === $map[((int) $number) % 23];
}

Recent comments

© 2014 Copyright by Keyvan Akbary