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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.