I have set up a function that gets a full name with X first names and last names and returns only the first name and last name. If you have a connective (de, da, do, etc.) before the last surname, it attaches to the result.
But the preg_match()
function is returning the expected expectation for 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);
Return:
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