Skip to content

Instantly share code, notes, and snippets.

@ewistrand
Last active February 17, 2020 20:11
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 ewistrand/15d2ddc717f74c2b46c4b19a2a6bf03e to your computer and use it in GitHub Desktop.
Save ewistrand/15d2ddc717f74c2b46c4b19a2a6bf03e to your computer and use it in GitHub Desktop.
Split Name Input into First, Midldle and Last names
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