Ah, now that's a whole 'nother problem...
Sometimes there's just one name, sometimes there's more than one, with commas seperating the names, yes?
Okay:
if(strpos($fullname,',')===false)
{ // There's only one name in the field. Handle this case just as you
// already are.
}
else
{ // There's more than one name. We want to deal with each name
// separately.
// Turn a long string of names into an array of individual names.
$fullnames=explode(',',$fullname);
// We'll put our transformed names in this array.
$names=array();
// Go through our list.
foreach($fullnames as $field)
{ // Pretty much as you already do:
$name = explode(' ', $field);
// Just have to strip extra spaces from the beginning and/or end of
// the name (since a comma is usually followed by a space).
$name=trim($name);
//Put the result into our array of transformed names.
$names[]= $name[0]." ".$name[1]{0};
}
// Take the array of transformed names, and glue them all together with
// ", ".
$name=join(', ',$names);
}
...that looks about right.
Strictly speaking, you could probably do away with checking to see whether the comma is there in the first place; but since I'm guessing that single-name entries are pretty common, if not dominant, it makes sense to make the check and avoid the extra exploding and foreaching and joining in such cases.