With this rule below, how do I show the first and last user name in the database?

Asked

Viewed 94 times

0

Inside the model, how do I show the user name: Example of the name that is in DB: José Alberto da Silva Nogueira. I would like you to show only José Nogueira.

public function getNome(){

    if($this->group == User::GROUP_NOME_CLIENTE)
        return $this->nome_cliente->ds_nome;

2 answers

2


Example using the array functions:

$str = 'José Alberto da Silva Nogueira';
$arr = explode(' ', $str);
echo ((count($arr) < 2)? $str : current($arr).' '.end($arr));

Example using string functions.

$str = 'José Alberto da Silva Nogueira';
echo strstr($str.' ', ' ', true).strrchr($str, ' ');

1

Follows code:

if($this->group == User::GROUP_NOME_CLIENTE) {
    $nomeCompleto = $this->nome_cliente->ds_nome;
    $separa = explode(" ", $nomeCompleto);
    if(count($separa) < 2){
       return $nomeCompleto;
    }
    return $separa[0] . " " . $separa[count($separa) -1];
}
  • the problem I still have is that if the user registers with a single name (José). Hence it is replicated the name.

  • I changed the code to foresee this case placed

Browser other questions tagged

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