Skip to content

Instantly share code, notes, and snippets.

@stilliard
Created April 19, 2012 12:47
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save stilliard/2420781 to your computer and use it in GitHub Desktop.
Save stilliard/2420781 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