Split Full Contact Name

Hello experts,

I am looking for a solution for my problem.

I like to be able to split the FULL NAME to 2 parts or 3 parts

For example
Will X Smith
Bill Gate

The script should be able to to detect the
First name as Will
Middle Name as X
Lastname as Smith

so i guess the pattern is maybe like this

Search for number of space in the contact name

If found 1 space in the contact name  then the first word is firstname  the second word is last name

If found 2 space in the contact name then the 1st word is Firstname, 2nd is Middle initial, 3nd is last name.

Thank you to all.



MantemperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian M.Web DeveloperCommented:
Hello! Hopefully this will help:

$name = "Will X Smith";

$names = strtok($name, " ");

$first_name = array_shift($names);
$last_name = array_pop($names);
$middle_names = implode(" ",$names);

This should give you following:
First Name: Will
Last Name: Smith
Middle Names: X
<?php
 
$name = "Will X Smith";
 
$names = strtok($name, " ");
 
$first_name = array_shift($names);
$last_name = array_pop($names);
$middle_names = implode(" ",$names);
 
echo "First Name: $first_name. Middle Names: $middle_names. Last Name: $last_name.";
 
?>

Open in new window

0
Julian M.Web DeveloperCommented:
Sorry, I forgot that strtok() does not actually create an array. Below is the corrected code.
<?php
 
$names = '';
 
$name = "Will X Smith";
 
$tok = strtok($name, " ");
 
while ($tok !== false) {
    $names[] = $tok;
    $tok = strtok(" ");
}
 
$first_name = array_shift($names);
$last_name = array_pop($names);
$middle_names = implode(" ",$names);
 
echo "First Name: $first_name<br />"
 . "Middle Names: $middle_names<br />"
 . "Last Name: $last_name";
 
?>

Open in new window

0
Julian M.Web DeveloperCommented:
Or an even easier method than using strtok() would be to use the explode() function - the reason I am creating an array is that I can easily get the first and the last array pieces. There are probably other ways to do this too.
<?php
 
$name = "Will X Smith";
 
$names = explode(" ",$name);
 
$first_name = array_shift($names);
$last_name = array_pop($names);
$middle_names = implode(" ",$names);
 
echo "First Name: $first_name<br />"
 . "Middle Names: $middle_names<br />"
 . "Last Name: $last_name";
 
?>

Open in new window

0
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Loganathan NatarajanLAMP DeveloperCommented:
i think this will work,

<?php

$names  = "Will X Smith";

$cont_name = explode(" ", $names);

echo $cont_name[0];
echo $cont_name[1];
echo $cont_name[2];

?>
0
dr_dedoCommented:
agree with logudotcom, added a little mod to his code to make things *better* if there is no middle name
<?php
$names  = "Will smith";
$cont_name = explode(" ", $names);
$fName = $cont_name[0]; 
$lastName = isset($cont_name[2]) ? $cont_name[2] : $cont_name[1];
$mName = isset($cont_name[2]) ? $cont_name[1] : null;
echo <<<NAMES
fName = $fName
mName = $mName
lastName = $lastName
NAMES;
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Loganathan NatarajanLAMP DeveloperCommented:
to Mantemper;

are you trying???
0
Cornelia YoderArtistCommented:
As others have said, use explode()

$namearray= explode(" ",$names);

Then you can find out how many parts to the name by

$numnameparts = count($namearray);

You can use each part of the name separately by (starting with 0 as the first part) by

$namepart = $namearray[0];  etc for each part 0,1,2,...

You can go through the array to check for titles such as "Dr", "Jr", etc by

for ($i=0;$i<count($namearray);$i++)
{
     //check $namearray[$i] for whatever you want
}

You can reassemble parts of the name using implode(), or by concatenation, for example

for ($i=0;$i<count($namearray);$i++)
{
     if ($namearray[$i] != "Dr"  && $namearray[$i] != "Jr")
         $newname.=$namearray[$i]." ";
}

This method gives you great flexibility for working with a name like you have.
0
MantemperAuthor Commented:

I'm sorry guys, I was a little busy.

Wow so many solutions.
Thank to all of you very much.
Let me try your script.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.