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,' '));
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.").
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With