Extract First / Last Name from Title
Last modified: August 26, 2009 - 03:48
I worked on a few sites that stored authors / persons in the normal form of First Name - Last Name:
Thomas H. Hauk
Fuzzy Dunlop
but needed to sort or modify these names as "Dunlop, Fuzzy" or "D. Fuzzy" so they created separate CCK fields for first_name and last_name. This worked (especially for integration with views) but I reconfigured these fields as computed_field types to save data entry time.
For the first name, I used a quick strpos of everything in the title in front of the last word (presumably surname):
$node_field[0]['value'] = substr($node->title, 0, strrpos($node->title, " "));
For the last name, I used another regex to grab the
preg_match('~([^\s]+)(?:,.*)?$~',$node->title, $match);
$node_field[0]['value'] = $match[0];