Behavior of the preg_match () function for abbreviation of names

1

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
    
asked by anonymous 25.07.2014 / 17:51

4 answers

1
$b = "da";
var_dump(preg_match('/^[^A-Z]*$/', $b{0}));

Your regex says, combine at the beginning of the line (% with_with%) something other than uppercase between ^ ( A-Z ) followed by any character ( [^A-Z] ), when * preg_match is passed ) applies regex only in% with% already you are passing the index of the string, the return is 1 because the combination happened.

    
25.07.2014 / 19:16
2

Your question has been answered, but you are predicting that the data will always be formatted as you want, ie first and last names in uppercase and lowercase in connectivity ...

so it would reduce the possibility of errors:

<?php
    function abrevia($var) {
    $nomes = explode(" ", $var);
    $a = reset($nomes);
    $c = end($nomes);
    if (count($nomes) > 2) {
        $b = prev($nomes);
        if (preg_match('/^[deaos]{2,3}$/i', $b)) {
            return implode(" ", array($a, $b, $c));
        }
    }
    return implode(" ", array($a, $c));
    }

    $a[] = "João Testador dos Testes";
    $a[] = "joão dos testis testando teste";
    $a[] = "João do Teste";
    $a[] = "João Testador Das Dores";
    $a[] = "João Testador de Melo";
    $a[] = "João Testador Da Silva";

    $b = array_map('abrevia', $a);

    var_dump($b);
?>

returns:

array(6) {
  [0]=>
  string(15) "João dos Testes"
  [1]=>
  string(10) "joão teste"
  [2]=>
  string(13) "João do Teste"
  [3]=>
  string(14) "João Das Dores"
  [4]=>
  string(12) "João de Melo"
  [5]=>
  string(13) "João Da Silva"
}
    
28.07.2014 / 02:18
0

Try this.

/**
 * Abreviar Nome
 * @param String $string Name user
 * @return string
 */
function FirstAndLastName($string) {
    $nome = explode(" ", $string);
    $first = $nome[0];
    $last = end($nome);
    if (count($nome) == 1) {
        $result = $nome[0];
    } else {
        $result = $first . ' ' . $last;
    }

    return $result;
}
    
25.07.2014 / 20:30
0

Do so

 $nome = 'JOAO DOS TESTES';
 $retira = array(' DE ', ' DOS ', ' E ', ' DO ', ' DA ', ' DAS ', ' DI ');
 $nomeLimpo = str_replace($retira, ' ', $nome);
    
11.08.2016 / 00:52