jeff5656 Posted January 26, 2011 Share Posted January 26, 2011 I have a field called full_name and it has John Smith as a value. I want to take John and put it to the first_name field and Smith to the last_name field. How do I do this, knowing that the first and last name are separateed by a space? Here's a query I did that does the same thing when they are separated by a COMMA, but I am not familiar with regular expressions enough to modify it to include space instead :-) $newlastname = $row['full_name']; if (preg_match('/^[^,]+(?=,)/', $row['full_name'], $match)) { $newlastname = $match[0]; } list($newlastname,$newfirstname) = explode(',',$row['full_name']); Link to comment https://forums.phpfreaks.com/topic/225728-use-regular-expression-to-break-up-full-name/ Share on other sites More sharing options...
litebearer Posted January 26, 2011 Share Posted January 26, 2011 $oldname = $row['full_name']; $temp_array = explode(" ", $oldname); $firstname = $temp_array[0]; $lastname = $temp_array[1]; Link to comment https://forums.phpfreaks.com/topic/225728-use-regular-expression-to-break-up-full-name/#findComment-1165470 Share on other sites More sharing options...
AbraCadaver Posted January 26, 2011 Share Posted January 26, 2011 Only because it's shorter list($firstname, $lastname) = explode(' ', $row['full_name']); Link to comment https://forums.phpfreaks.com/topic/225728-use-regular-expression-to-break-up-full-name/#findComment-1165537 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.