Extract First / Last Name from Title
Last updated on
30 April 2025
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];Help improve this page
Page status: Not set
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion