View source
<?php
function _field_nif_validate_dni_cif_nie($cif, $options = array()) {
$cif = strtoupper($cif);
$num = array();
for ($i = 0; $i < 9; $i++) {
$num[$i] = substr($cif, $i, 1);
}
if (!preg_match('/((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)/', $cif)) {
return FALSE;
}
if (isset($options['nif']) && $options['nif'] === 'nif') {
if (preg_match('/(^[0-9]{8}[A-Z]{1}$)/', $cif)) {
if ($num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', substr($cif, 0, 8) % 23, 1)) {
$last_letter = $num[8];
array_pop($num);
return array(
'last_letter' => $last_letter,
'number' => implode('', $num),
'type' => 'NIF',
);
}
else {
return FALSE;
}
}
}
$suma = $num[2] + $num[4] + $num[6];
for ($i = 1; $i < 8; $i += 2) {
$suma += substr(2 * $num[$i], 0, 1) + substr(2 * $num[$i], 1, 1);
}
$n = 10 - substr($suma, strlen($suma) - 1, 1);
if (isset($options['nif']) && $options['nif'] === 'nif') {
if (preg_match('/^[KLM]{1}/', $cif)) {
if ($num[8] == chr(64 + $n)) {
$last_letter = $num[8];
$first_letter = $num[0];
array_pop($num);
array_shift($num);
return array(
'first_letter' => $first_letter,
'last_letter' => $last_letter,
'number' => implode('', $num),
'type' => 'NIF',
);
}
else {
return FALSE;
}
}
}
if (isset($options['cif']) && $options['cif'] === 'cif') {
if (preg_match('/^[ABCDEFGHJNPQRSUVW]{1}/', $cif)) {
if ($num[8] == chr(64 + $n) || $num[8] == substr($n, strlen($n) - 1, 1)) {
$last_letter = $num[8];
$first_letter = $num[0];
array_pop($num);
array_shift($num);
return array(
'first_letter' => $first_letter,
'last_letter' => $last_letter,
'number' => implode('', $num),
'type' => 'CIF',
);
}
else {
return FALSE;
}
}
}
if (isset($options['nie']) && $options['nie'] === 'nie') {
if (preg_match('/^[T]{1}/', $cif)) {
if ($num[8] == preg_match('/^[T]{1}[A-Z0-9]{8}$/', $cif)) {
$last_letter = $num[8];
$first_letter = $num[0];
array_pop($num);
array_shift($num);
return array(
'first_letter' => $first_letter,
'last_letter' => $last_letter,
'number' => implode('', $num),
'type' => 'NIE',
);
}
else {
return FALSE;
}
}
if (preg_match('/^[XYZ]{1}/', $cif)) {
if ($num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', substr(str_replace(array(
'X',
'Y',
'Z',
), array(
'0',
'1',
'2',
), $cif), 0, 8) % 23, 1)) {
$last_letter = $num[8];
$first_letter = $num[0];
array_pop($num);
array_shift($num);
return array(
'first_letter' => $first_letter,
'last_letter' => $last_letter,
'number' => implode('', $num),
'type' => 'NIE',
);
}
else {
return FALSE;
}
}
}
return FALSE;
}