Created
April 19, 2012 12:47
-
-
Save stilliard/2420781 to your computer and use it in GitHub Desktop.
PHP - Get a users first name from the full name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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