My PHP function that reads names and attempts to fix the suffixes I am reading a text file that has some name inconsistencies. For example I have names like:

MILLERS, WALTER M IV
DUPONTE, THOMAS B. II
HARDIWAY, GRANT U. SR.
GUIDRY, PAUL JOHN


As you can see I can split the name by first name and last name however the suffixes are placed incorrectly. Ideally the names should be:

WALTER M MILLERS IV
THOMAS B. DUPONTE II
GRANT U. HARDIWAY SR.
PAUL JHN GUIDRY


That being said this is what I have but I feel Im over complicating something simple. Any tips? Also my logic does not account for the suffix of V since that could be a middle initial also. 

private function extractFullName($name){

        $suffix = collect(['II', 'III', 'IV', 'JR.', 'SR.']);

        // lets first assemble a full name from the row
        $fullNameArray = explode(",", $name);
        $firstName=trim($fullNameArray[1]);
        $lastName=trim($fullNameArray[0]);

        //lets look at the FIRST NAME for II/III/IV/JR./SR.
        $suffix-&gt;each(function ($item, $key) use ($firstName, $lastName) {

            if (strpos($firstName, " ".$item)){
                $firstName = preg_replace('/ '.$item.'$/', '', $firstName);
                $lastName.=' '.$item;
                return false;
            }
        });

        //Basically arranging it in a 'firstname lastname II/III/IV/JR./SR.' format
        $name = $firstName." ".$lastName;
        $name = ucwords(strtolower($name));
        return $name;
    }
If it can be assumed that a suffix will always have a space before it this is easier. 

Loop through suffixes and see if one is in the first name.  Strip out the suffix if is is there.  Any suffix that is found is added to a suffix variable, s.  

Treating the suffix separately is nice because it can be added, separated, lower cased, modified etc.  For example, suffixes could be standardized; "SR." and "SR" could all be "SR." in the final name. This is easier to do if suffix is a separate variable.  It could be an array as well. 

function extractFullName($name) {

    // $suffixes = ['II', 'III', 'IV', 'JR.', 'SR.'];
    // Suffixes would always have a space before them
    $suffixes = [' II', ' III', ' IV', ' JR.', ' SR.'];


    // lets first assemble a full name from the row
    $fullNameArray = explode(",", $name);
    $firstName = trim($fullNameArray[1]);
    $lastName = trim($fullNameArray[0]);
    $s = ''; // default zero length string.
    // 
    //lets look at the FIRST NAME for II/III/IV/JR./SR.
    foreach ($suffixes as $suffix) {
        if (strpos($firstName, $suffix)) {
            // Suffix was found, remove it from first name
            $firstName = str_replace($suffix, "", $firstName);
            $s .= $suffix;  // keep ths suffixes stored separately.
        }
    }

    //Basically arranging it in a 'firstname lastname II/III/IV/JR./SR.' format
    $fullName = $firstName . " " . $lastName;
    $finalName = ucwords(strtolower($fullName)) . " " . $s;
    return $finalName;
}

$name1 = 'MILLERS, WALTER M IV';
$name2 = 'MIVERS, LIVEY M IV';  // test for IV in the middle of a name
$name3 = 'DUPONTE, THOMAS B. II';
$name4 = 'DUPONTE, THOMAS B. II SR.';  // test for multiple suffixes.
$name5 = 'HARDIWAY, GRANT U. SR.';
$name6 = 'GUIDRY, PAUL JOHN';
echo extractFullName($name1) . "&lt;br&gt;";
echo extractFullName($name2) . "&lt;br&gt;";
echo extractFullName($name3) . "&lt;br&gt;";
echo extractFullName($name4) . "&lt;br&gt;";
echo extractFullName($name5) . "&lt;br&gt;";
echo extractFullName($name6) . "&lt;br&gt;";