Link to home
Start Free TrialLog in
Avatar of Mantemper
Mantemper

asked on

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.



Avatar of Julian Matz
Julian Matz
Flag of Ireland image

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

SOLUTION
Avatar of Julian Matz
Julian Matz
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
to Mantemper;

are you trying???
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mantemper
Mantemper

ASKER


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.