A modern PHP 7.0+ Spanish NIF (Número de Indentifación Fiscal) validator.
Other NIF validators we saw either had really obscure code or just implemented a validator for the DNI/NIE, not the CIF.
Using Composer:
composer require ulabox/nif-validator
<?php
use NifValidator\NifValidator;
//CIF
assert(NifValidator::isValid('B65410011'));
//DNI
assert(NifValidator::isValid('93471790C'));
//NIE
assert(NifValidator::isValid('X5102754C'));
Starting from version 1.1.x you can also separetely validate personal and entity nifs
<?php
use NifValidator\NifValidator;
//CIF
assert(NifValidator::isValidEntity('B65410011'));
//DNI
assert(NifValidator::isValidPersonal('93471790C'));
//NIE
assert(NifValidator::isValidPersonal('X5102754C'));
Starting from version 1.2.x you can also separetely validate individual nif types
<?php
use NifValidator\NifValidator;
//CIF
assert(NifValidator::isValidCif('B65410011'));
//DNI
assert(NifValidator::isValidDni('93471790C'));
//NIE
assert(NifValidator::isValidNie('X5102754C'));
This validator does not strip or uppercase any character, it's your responsibility to previously filter the input.