Skip to content

Instantly share code, notes, and snippets.

@jamiepittock
Forked from stilliard/fullNameToFirstName.php
Created November 24, 2022 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiepittock/4460bdd06488af023d731a9fe377a9d6 to your computer and use it in GitHub Desktop.
Save jamiepittock/4460bdd06488af023d731a9fe377a9d6 to your computer and use it in GitHub Desktop.
PHP - Get a users first name from the full name
<?php
/**
* Get a users first name from the full name
* or return the full name if first name cannot be found
* e.g.
* James Smith -> James
* James C. Smith -> James
* Mr James Smith -> James
* Mr Smith -> Mr Smith
* Mr J Smith -> Mr J Smith
* Mr J. Smith -> Mr J. Smith
*
* @param string $fullName
* @param bool $checkFirstNameLength Should we make sure it doesn't just return "J" as a name? Defaults to TRUE.
*
* @return string
*/
function fullNameToFirstName($fullName, $checkFirstNameLength=TRUE)
{
// Split out name so we can quickly grab the first name part
$nameParts = explode(' ', $fullName);
$firstName = $nameParts[0];
// If the first part of the name is a prefix, then find the name differently
if(in_array(strtolower($firstName), array('mr', 'ms', 'mrs', 'miss', 'dr'))) {
if($nameParts[2]!='') {
// E.g. Mr James Smith -> James
$firstName = $nameParts[1];
} else {
// e.g. Mr Smith (no first name given)
$firstName = $fullName;
}
}
// make sure the first name is not just "J", e.g. "J Smith" or "Mr J Smith" or even "Mr J. Smith"
if($checkFirstNameLength && strlen($firstName)<3) {
$firstName = $fullName;
}
return $firstName;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment