1
I set up a function that takes a full name with X names and surnames and returns only the first and last names. If you have a connective (from, from, from, dos, etc.) before the last surname, it attaches in the result.
But the function preg_match()
, is returning the opposite expected by my regex
.
Example:
function abrevia($var) {
$nomes = explode(" ", $var);
$a = reset($nomes);
$c = end($nomes);
if (count($nomes) > 2) {
$b = prev($nomes);
if (preg_match('/^[^A-Z]*$/', $b{0})) {
return implode(" ", array($a, $b, $c));
}
}
return implode(" ", array($a, $c));
}
$a[1] = "João Testador dos Testes";
$a[2] = "João dos Testis Testando do Teste";
$a[3] = "João do Teste";
$b = array_map('abrevia', $a);
var_dump($b);
Returns:
array (size=3)
1 => string 'João dos Testes' (length=16)
2 => string 'João do Teste' (length=14)
3 => string 'João do Teste' (length=14)
Doubt:
This function below should not return false
(0)?
$b = "da";
var_dump(preg_match('/^[^A-Z]*$/', $b{0})); //retorna 1
It is because it comes from an Active Directory, where data is standardized, there is no such possibility because it would break a registration rule.
– Marcelo Aymone