I’m working with the Twitter API which provides only a single field called name, which includes the full name of a person.
The application that we’re constructing requires a first name and a last name.
Now my question is how I am able to extract the first and last name from a single name field.
I know that I can simply just split or explode the string but this won’t work if the person for example has first name which consists of more than one word.
2
There are lots of simple ways of doing this wrong – read this to see why. There is no way of getting it right programmatically.
That said, some of the wrong ways are not that bad, all things considered. If you can live with making occasional errors and correcting them with a list of exceptions after users notify you, then naive splitting on white space might be the right thing to do. But you must exercise your judgement whether that’s the right trade-off for your application.
2
I would consider using a regular expression pattern with grouping to extract only the beginning and the end words.
2