I am stuck with a NAME field, which typically is in the format:
FirstName LastName
However, I also have the occasional names that are in any of these formats (with prefix or suffix):
Mr. First Last
First Last Jr.
What do people think is a safe way to split these into FIRST/LAST name variables in PHP? I can’t really come up with anything that tends to work all of the time…
A regex is the best way to handle something like this.
Try this piece – it pulls out the prefix, first name, last name and suffix:
The result comes out like this:
etc…
so in the array
$array[0]contains the entire string.$array[2]is always first name and$array[3]is always last name.$array[1]is prefix and$array[4](not always set) is suffix.I also added code to handle both ‘ and ’ for names like Shaqueal O’neal and D’angelo Hall.