4

After having read a few questions already asked and checking a few other sites I'm still no further forward in finding a simple way to take a full name like say "Jake Whiteman" and trimming it down so that it would be shown on the web page as "Jake W." obviously without the speech marks round it just so there isn't any confusion there.

Anyone have any idea how to do it? I'm sure its probably just a matter of finding the white space and then trimming down the surname, I just can't seem to find a way of doing it.

Thanks in advance!

Geordie Dave
  • 65
  • 3
  • 8
  • [Assuming that everyone has a forename and surname is incorrect](https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/). – Raedwald Oct 01 '19 at 10:44
  • See also https://stackoverflow.com/questions/1122328/first-name-middle-name-last-name-why-not-full-name – Raedwald Oct 01 '19 at 10:45

5 Answers5

7
$names = explode( " ", $name );
echo $names[0]." ".$names[1][0];
sean
  • 3,955
  • 21
  • 28
5

You can use this code. This will work for 3 worded names where assuming that the second word is the middle name, only the last name will be truncated.

<?php

$name = "Jake Awesome Whiteman";
$separate = explode(" ", $name);
$last = array_pop($separate);

echo implode(' ', $separate)." ".$last[0].".";

?>
Kartik
  • 9,463
  • 9
  • 48
  • 52
2

Assuming they're always of the format "[Other Names] LastName", such that the last word is always the last name, you can split it into tokens by the delimeter [space] by the php function explode() (http://php.net/manual/en/function.explode.php)

// Given
$name = "Jake Whiteman";

// Process
// Tokenize, getting separate names
$names = explode(' ', $name);
// Pop last name into variable $last_name, keep remaining names in $names
$last_name = array_pop($names);
// Get last initial
$last_initial = $last_name[0];

// Put first names back together
$beginning = implode(' ', $names);
$full_name = $beginning.' '.$last_initial.'.';

You can put this all together in a function:

function nameWithLastInitial($name) {
    $names = explode(' ', $name);
    $last_name = array_pop($names);
    $last_initial = $last_name[0];
    return implode(' ', $names).' '.$last_initial.'.';
}
$name = "Jake Whiteman";
echo nameWithLastInitial($name); // Should print 'Jake W.'
garromark
  • 814
  • 8
  • 20
  • This is much more robust than my version. – sean Jul 13 '12 at 02:57
  • 1
    Still makes quite a few assumptions about string format and array lengths :). Robustness is only useful relative to need. If you can assume a simpler solution, go for it, it will usually have better performance anyway. – garromark Jul 13 '12 at 02:59
1
$name = "Jake Whiteman";
$names = explode(' ', $name); //$names[0] = "Jake", $names[1] = "Whiteman"
echo $names[0]." ".substr($names[1], 0,1).".";
nine7ySix
  • 486
  • 2
  • 13
1

this would be helpful when only first name and user enter multiple name

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php

$userName = "Jakeds hfg gsd";
if(preg_match('/\s/',$userName)) {
$separate = explode(" ", $userName);
$last = array_pop($separate);
$first = mb_strimwidth($separate[0], 0 , 15);
echo $first." ".$last[0].".";
} else {
$first = mb_strimwidth($userName, 0 , 15);
echo $first;
}

//echo $first." ".$last[0].".";

?>
</body>
</html>
sunil
  • 832
  • 15
  • 25