How can I abbreviate a name to 2 words, ignoring (from, da, dos, das)?

Asked

Viewed 748 times

2

I have a user table on the system where the full user name is registered. But, when displaying these names in the system, I should only use two words of that person’s name to display. And when the words das, dos, da, do and de appear, I need those words to be ignored the next name is captured. But it should always display only 2 words of the person’s name.

Examples:

 'Márcio da Silva da Costa' => 'Márcio Silva'
 'Lucas Oliveira Xavier'    => 'Lucas Oliveira'
 'Wallace de Souza'         => 'Wallace Souza'

How can I do this in PHP?

I currently have a code that does this, but I would like something simpler than that:

function nameSlice($name, $int = 2)
{
    $ignore = array('e', 'de', 'da', 'do', 'dos', 'das', 'a', 'le');

    $sliceName = explode(' ', $name);

    foreach ($sliceName as $key => $value) {
        if (in_array(strtolower($value), $ignore) && $int != $key) {
            $int++;
        }
    }

    $sliceName = array_slice(array_filter($sliceName), 0, $int);

    if (in_array(strtolower(end($sliceName)), $ignore, true)) {
        array_pop($sliceName);
    }

    return implode(' ', $sliceName);
}
  • What have you done? : D Are you sure you’re going to ignore the last name? It’s usually the most important one. But not always. So automating this can produce useless results.

  • Demands of my boss, @bigown!

  • @bigown, I could tell there’s a scam in my code, right. This was the time I was creating a class called Util and created some static method in it.

  • Gambiarra is normal, if time allows, I take a look.

  • Should you only get the first and last names? Or the last?

  • @Marcelodeandrade, as in the example shown in the question. First and second. If the second word is de|das?|dos?, then we ignore that word and skip to the next.

  • 1

    @bigown, I got this http://ideone.com/NdLDPX

  • I think this question was good for me. It forced me to refactor systems resources (which I did myself) that are outdated. The first step towards healing is admitting the problem. In this case, I admit my gambit.

Show 3 more comments

1 answer

2


A simple regular expression method will already solve your problem:

function removeDaDeDiDoDu($name) {
           $name = preg_replace('/\s(d[A-z]{1,2}|a(.){1,2}?|e(.){1,2}?|le{1}|[A-z.]{1,2}\s)/i',' ',$name);
           return preg_replace('/\s+/i',' ', $name);
}

Here a working example: http://ideone.com/ow9LSN

Now if you just want two names, just take the first and the last:

$nome = 'Wallace de Souza Vizerra';
$allNames = explode(' ', $nome);
$nome = $allNames[0].' '.$allNames[count($allNames)-1];
echo $nome;

Here the example: http://ideone.com/yVBZhx

  • I liked its function. It worked. The only problem is that in 'wallace de soza vizerra' he converts to 'wallace souza vizerra' with two spaces where there was the de|da|do

  • Yes, I removed the double spaces, look there.

  • If you want to improve the expression, here is the way: http://regexr.com/3bt9u

  • 1

    Thanks for the reply. Just a tip. \s\s+ is not necessary, only \s+.

  • fact. the + already does it

Browser other questions tagged

You are not signed in. Login or sign up in order to post.