Jump to content

[SOLVED] parseing names


curtm

Recommended Posts

I have some emails that I'm parsing into a database.  In those emails, there are names. the names have no controls on the way they are typed in, so I have data like so...

 

fname m lname

fname m. lname

fname m lname suffix

fname mname lname

fname lname

fname lname suffix

 

How can I parse this so I get the right data in the right field?  I have fname m lname for my database fields.

 

thanks!

Link to comment
Share on other sites

If the names themselves don't have have space within them then you could use explode to split the names into three variables, like so:

 

$name = 'fname m lname';

list($fname, $mname, $lname) = explode(' ', $name);

echo 'Firstname: ' . $fname . '<br />';
echo 'Middlename: ' . $mname . '<br />';
echo 'Surname: ' . $lname;

Link to comment
Share on other sites

I'm using the EXPLODE function now, but that doesn't help me get the names in the right fields.

 

If one guy puts fname lname and the next guy puts fname m lname, the explode would have to have conditions for the names to go in the right field.

Link to comment
Share on other sites

You could check to see how many parts have been returned from explode, if its two then fname lname if three fname m lname.

 

$name = 'fname lname';

$parts = explode(' ', $name);

switch(count($parts))
{
    case 2:
        list($fname, $lname) = explode(' ', $name);
        $mname = 'N/A';
    break;

    case 3:
        list($fname, $mname, $lname) = explode(' ', $name);
    break;
}

echo 'Firstname: ' . $fname . '<br />';
echo 'Middlename: ' . $mname . '<br />';
echo 'Surname: ' . $lname;

 

 

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.