Last active
February 17, 2020 20:11
-
-
Save ewistrand/15d2ddc717f74c2b46c4b19a2a6bf03e to your computer and use it in GitHub Desktop.
Split Name Input into First, Midldle and Last names
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
if(! function_exists('split_name')) { | |
function split_name($name) { | |
$parts = array(); | |
while ( strlen( trim($name)) > 0 ) { | |
$name = trim($name); | |
$string = preg_replace('#.*\s([\w-]*)$#', '$1', $name); | |
$parts[] = $string; | |
$name = trim( preg_replace('#'.$string.'#', '', $name ) ); | |
} | |
if (empty($parts)) { | |
return false; | |
} | |
$parts = array_reverse($parts); | |
$name = array(); | |
$name['first_name'] = $parts[0]; | |
$name['middle_name'] = (isset($parts[2])) ? $parts[1] : ''; | |
$name['last_name'] = (isset($parts[2])) ? $parts[2] : ( isset($parts[1]) ? $parts[1] : ''); | |
return $name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment