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
https://forums.phpfreaks.com/topic/80315-solved-parseing-names/
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
https://forums.phpfreaks.com/topic/80315-solved-parseing-names/#findComment-407045
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
https://forums.phpfreaks.com/topic/80315-solved-parseing-names/#findComment-408177
Share on other sites

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.