Only take the user’s name and surname

Asked

Viewed 3,595 times

3

Dear colleagues.

How could I take a user’s name and surname? I usually use it as follows:

$nomeUsuario = 'Francisco de Assis';
list($nome,$sobrenome) = explode(' ',$nomeUsuario);

But this way I’d take Francis of and I would actually like to catch Francisco Assis.

Would anyone have any idea how I would do that?

Thank you!

1 answer

7


Try it like this

$partes = explode(' ', $nomeUsuario);
$primeiroNome = array_shift($partes);
$ultimoNome = array_pop($partes);
  • array_shift - remove and return the first array value.
  • array_pop - remove and return the last array value.
  • Thanks Maicon. It worked!!

Browser other questions tagged

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