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

0

Within the model, how do I display the user name: Example of the name that is in the 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;
    
asked by anonymous 29.12.2015 / 17:11

2 answers

2

Example using 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, ' ');
    
29.12.2015 / 18:29
1

Follow the code below:

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];
}
    
29.12.2015 / 18:10