Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get last name first inital [duplicate]

Tags:

php

strip

Possible Duplicate:
Regex - Return First and Last Name

Hi,

I have the full name of users stored in one field. How can i get the full first name and last name first initial. This is what i am using right now:

 $first = substr($full_n, 0, strpos($full_n,' '));  
like image 297
AAA Avatar asked Dec 28 '22 21:12

AAA


1 Answers

list ($first, $last) = explode(' ', $full_n, 2);
echo "$first {$last[0]}.";

Will not work with more than two names too, like all the other solutions. You should save all (firstname, middle name, surname, ...) separatly, if you want to do more with it, than just displaying.

Update:

Inspired be Pekka (comments of the question ;))

$names = explode(' ', $full_n);
$first = array_shift($names);
$last = array_pop($names);
echo "$first {$last[0]}.";

This works for an arbitrary (>= 2) count of names, but will only display the first (complete) and the last (shortened). Something like "Schmidt-Mueller" will look curious ("S.").

like image 188
KingCrunch Avatar answered Dec 30 '22 10:12

KingCrunch