Jump to content

use regular expression to break up full name


jeff5656

Recommended Posts

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']);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.