parsing a fullname in php?
I have a full_name
field in a mysql database and I need to parse out the first and last names be开发者_如何转开发cause I am now transferring the data into first_name
and last_name
fields
So the names are like this; some have middle names and some don't:
James K. Dillon
Mark Holder
Tiffini lynn Jones
I found the php function strtok that may help but does anyone have other ideas?
Well.. identifying names may be pretty hard, in particular if users are prone to typos.
A good approach that may not work in 100% of cases (let's say at least 90% or for all the few examples you shown) is to divide the string into an array and get the first and last element.
$name_parts = explode(' ', $name);
$first_name = $name_parts[0];
$last_name = $name_parts[sizeof($name_parts)-1];
I preferred using explode
instead of strtok
To put it short, you're pretty much doomed.
Some people have just first and last name: John Smith
Some have first, middle and last: John Philip Smith
Some have first name, but their last name consist of two parts: John Smith Parker
(and yes, some people do not use a hyphen for that)
Some cultures put last (i.e. family) name before first: Kurosawa Akira
etc...
Jason Priem wrote an excellent Human Name Parser that takes names of various complexities and formats like:
- J. Walter Weatherman
- de la Cruz, Lupe
- George Oscar “Gob” Bluth, Jr.
and parses out the:
- leading initial (Like “J.” in “J. Walter Weatherman”)
- first name (or first initial in a name like ‘R. Crumb’)
- nicknames (like “Gob” in “George Oscar “Gob” Bluth, Jr.”)
- middle names
- last name (including compound ones like “van der Sar’ and “Ortega y Gasset”), and
- suffix (like ‘Jr.’)
Why don't you search for the number of spaces.
If there is one space, you know it's two names, first/last, if two spaces, you know there's 3 names, and you can do what you want with the middle name or keep it and attach it to one of the others.
Another option is to use explode using the delimiter space ' '
$name = "James K. Dillon"
$array = explode($name,' ');
// $array = {'James', 'K.', 'Dillon'}
Edit: I've just see slandau answer, so using his idea and mine:
$name = "James K. Dillon"
$array = explode($name,' ');
if (array_length($array) < 3){
// it hasn't got middle name
}
else{
// it has middle name
}
ok so i have a full_name field in a mysql db
Bad Idea. We've got this on our systems and it's a nightmare, how are you going to order the entries my surname? If you ever want to interoperate with any other system, you'll have to use logic to guess the split, and this will be inaccurate. You're much better off having at least forename and surname fields.
since you're deviding the name into first_name and last_name fields i assume the middle name if available should be within the last_name field? if so explode will work but would require a limit of 2, otherwise the last_name would be left out
<?php
$name="Peter L. Panda";
$temp=explode(" ",$name,2);
$firstname=$temp[0]; //==Peter
$lastname=$temp[1]; //==L. Panda
?>
Use explode(" ",$name)
and then take the first and last indexes from the array.
精彩评论