How to redeem from full name database and show only first name?

Asked

Viewed 46 times

1

I have a table that saves me users and their data, name and saved in a single field. What I want is to take a name from the database for example César Sousa and show only César I am using Laravel 5.4 and rescues the user name through {{Session::get('nome')}} which is a secret I keep when I log in. I wanted the time I get the username in the Blade template to show me the first name before the first space.

  • Show the code you’ve tried

1 answer

1

What you want is to get the first name before the first white space. In PHP there are a few ways to do this. The simplest and most common solution to this problem is using EXPLODE (http://php.net/manual/en/function.explode.php).

This method transforms a string into an array, separating each part of the array from a substring.

Example:

explode(' ', 'Carlos Souza de Azevedo'); // ['Carlos', 'Souza', 'de', 'Azevedo']

In this scenario, you want to get the first name. That is, you want to get the first item of the array.

$nomes = explode(' ', 'Carlos Souza de Azevedo');
$primeiroNome = $nomes[0]; // 'Carlos'
  • I had to test I am using I used the explode when showing the name of the user that resgato a Sesssion but it does not work I error when opening the page

Browser other questions tagged

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