Split Nicename into FIrst & Last name
this is in continuation of Javascript Regular Expression to attempt to split name into Title/First Name(s)/Last Name
I have first_name & last_name in my database and user can enter his fullname as per designs. Its a single input field where he can enter name in any format e.g.
- Dr. James Watson
- hi开发者_运维百科lly billy
- Sir Lorenzo Von Matterhorn
etc.
now question is, 1. How to instantly validate the name, regex? 2. How to process it into first_name and last_name. I can either use JS or PHP
is there any particular set of rules laid out for such a purpose?
There is no international standard in naming people. But, maybe the North Koreans have one:
Kim
+ level of devotion to the leader
.
People may have multiple first names and even multiple last names.
The order of names might differ even in the same culture: http://en.wikipedia.org/wiki/Chinese_name (Western Chineses swap first and last to fit the English convention).
There are formal addresses such as Mr. or Sir.
There are academic titles in unlimited combination: Prof., Dr., ..., http://en.wikipedia.org/wiki/Title#Academic_titles
There can be generation suffixes (Junior, Senior): http://en.wikipedia.org/wiki/Junior_%28suffix%29#Generational_titles
The world's largest name is:
Adolph Blaine Charles David Earl Frederick Gerald Hubert Irvin John Kenneth Lloyd Martin Nero Oliver Paul Quincy Randolph Sherman Thomas Uncas Victor William Xerxes Yancy Zeus Wolfeschlegelsteinhausenbergerdorffvoralternwarengewissenhaftschaferswessenschafewarenwohlgepflegeundsorgfaltigkeitbeschutzenvonangreifendurchihrraubgierigfeindewelchevoralternzwolftausendjahresvorandieerscheinenwanderersteerdemenschderraumschiffgebrauchlichtalsseinursprungvonkraftgestartseinlangefahrthinzwischensternartigraumaufdersuchenachdiesternwelchegehabtbewohnbarplanetenkreisedrehensichundwohinderneurassevonverstandigmenschlichkeitkonntefortplanzenundsicherfreuenanlebenslanglichfreudeundruhemitnichteinfurchtvorangreifenvonandererintelligentgeschopfsvonhinzwischensternartigraumen, Senior.
If your input field is rectricted to a max number of chars, no worries, this person fortunately has a short name:
Wolfe+585, Senior
In that case you should not forget to allow +
and 585
to be valid name chars.
http://en.wikipedia.org/wiki/Wolfe%2B585,_Senior
Sounds like homework. Seems doubtful in the general case. See http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/
Most web forms have a spot for first name, last name, etc. If it was easy they wouldn't do that.
Yoda says: Invoke the regex!? Descend you will into a hell of special cases. Stop before it is too late.
Dr John Smith
John Smith, PhD
John Smith, MD
Dr John Smith MD
King James VI
James VI, King of Blah Blah Blah, Defender of the Faith
Publius Cornelius Scipio Africanus
Pope Benedict
Benedict Arnold
Jim Pope
Sister Theresa
Mother Theresa
Mother !@#$%^
Twisted Sister
The Rev. Dr. Martin Luther King Jr
The Rev Dr Martin Luther King Jr
Dr Martin Luther King, Jr
President George W Bush
W
Boy George
This is a tricky problem with no universal solution -- as others have pointed out. Probably the best approach is to allow the user to enter the title, first name, and last name separely. However, if you really need to do the parsing, there are some simple solutions that might work for at least the most common name formats. Here's one example:
$name = "Dr. James Watson";
// Define the set of allowed titles
$titles = 'dr|dr\.|prof|prof\.|sir';
// If the name is composed of two words separated by a space, assume this is
// first and last name
if (preg_match('/^([[:alpha:]]+) ([[:alpha:]]+)$/', $name, $matches)) {
$first_name = $matches[1];
$last_name = $matches[2];
}
// If there are more than two parts, check if the first part is the title
elseif (preg_match('/^(' . $titles . ')? ?([[:alpha:]]+) ([[:alpha:] ]+)$/i', $name, $matches)) {
$prefix = $matches[1];
$first_name = $matches[2];
$last_name = $matches[3];
}
else {
// Name cannot be parsed
}
精彩评论