Using PHP to Split full names in to first name last name combo

So often we find ourselves importing data that has first, middle and last names combined in one field,. This is annoying and can be corrected.

function splitName( $which, $what ){
$parts = explode(” “, $what);
$retStr = “”;
if( $which == ‘first’ ){
if( sizeof( $parts ) == 2 ){
$retStr .= $parts[0];
} else if( sizeof( $parts ) >= 3 ){
$retStr .= $parts[0] . ” ” . $parts[1];
} else {
$retStr .= $parts[0];
}
} else if( $which == ‘last’ ){
if( sizeof( $parts ) == 2 ){
$retStr .= $parts[1];
} else if( sizeof( $parts ) == 3 ){
$retStr .= $parts[2];
} else if( sizeof( $parts ) > 3 ){
// if more than 3 parts,. then the last name gets the final two parts only as the first got the first two parts
for( $i=(sizeof( $parts )-2 ); $i

Comments are closed.