Hmm...pseudocode follows...
1) Count the members of the array,
2) Check that the last array member isn't all "I"s
3a) if it is all "I"s, take the second-to-last member, and put it in $lastname
3b) if it isn't all "I"s, put it in $lastname
4) print $lastname, followed by each other array member in order, excluding the one that matches $lastname
so...
$pieces = explode(" ", $name);
$count = count($pieces);
if (preg("I+$", $pieces[$count - 1])) {
$lastname = $pieces[$count - 2];
} else {
$lastname = $pieces[$count - 1];
}
$fmt_name .= $lastname.",";
for ($i = 0; $i < $count; $i++ ) {
if ($pieces[$i] != $lastname) {
$fmt_name .= $pieces[$i]." ";
}
}
echo $fmt_name;
This should give you:
Lastname, Firstname whatever whatever...
Of course, this is off the top of my head 😉