forked from joshfraser/PHP-Name-Parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.php
187 lines (166 loc) · 6.83 KB
/
parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
// split full names into the following parts:
// - prefix / salutation (Mr., Mrs., etc)
// - given name / first name
// - middle initials
// - surname / last name
// - suffix (II, Phd, Jr, etc)
class FullNameParser
{
public function split_full_name($full_name) {
$full_name = trim($full_name);
// setup default values
extract( array( 'salutation' => '','fname' => '', 'initials' => '', 'lname' => '', 'suffix' => '' ));
if ( preg_match("/(.*), *(.*)/", $full_name, $matches) ) {
$full_name = $matches[1];
$final_suffix = $matches[2];
} else {
$final_suffix = '';
}
// split into words
$unfiltered_name_parts = explode(" ",$full_name);
// completely ignore any words in parentheses or quotes
foreach ($unfiltered_name_parts as $word) {
if ($word{0} != "(" && $word{0} != '"')
$name_parts[] = $word;
}
// is the first word a title? (Mr. Mrs, etc), or more than one of the above?
$salutation = '';
while ( $s = $this->is_salutation($name_parts[0]) ) {
$salutation .= "$s ";
array_shift($name_parts);
}
trim($salutation);
$suffix = '';
while ( $s = $this->is_suffix($name_parts[sizeof($name_parts)-1]) ) {
$suffix .= "$s ";
array_pop($name_parts);
}
$suffix .= $final_suffix;
trim($suffix);
// set the range for the middle part of the name (trim prefixes & suffixes)
$start = 0;
$end = sizeof($name_parts);
// concat the first name
for ($i=$start; $i < $end-1; $i++) {
$word = $name_parts[$i];
// move on to parsing the last name if we find an indicator of a compound last name (Von, Van, etc)
// we use $i != $start to allow for rare cases where an indicator is actually the first name (like "Von Fabella")
if ($this->is_compound_lname($word) && $i != $start)
break;
// is it a middle initial or part of their first name?
// if we start off with an initial, we'll call it the first name
if ($this->is_initial($word)) {
// is the initial the first word?
if ($i == $start) {
// if so, do a look-ahead to see if they go by their middle name
// for ex: "R. Jason Smith" => "Jason Smith" & "R." is stored as an initial
// but "R. J. Smith" => "R. Smith" and "J." is stored as an initial
if ($this->is_initial($name_parts[$i+1]))
$fname .= " ".strtoupper($word);
else
$initials .= " ".strtoupper($word);
// otherwise, just go ahead and save the initial
} else {
$initials .= " ".strtoupper($word);
}
} else {
$fname .= " ".$this->fix_case($word);
}
}
// check that we have more than 1 word in our string
if ($end-$start > 1) {
// concat the last name
for ($i; $i < $end; $i++) {
$lname .= " ".$this->fix_case($name_parts[$i]);
}
} else {
// otherwise, single word strings are assumed to be first names
$fname = $this->fix_case($name_parts[$i]);
}
// return the various parts in an array
$name['salutation'] = $salutation;
$name['fname'] = trim($fname);
$name['initials'] = trim($initials);
$name['lname'] = trim($lname);
$name['suffix'] = $suffix;
return $name;
}
// detect and format standard salutations
// I'm only considering english honorifics for now & not words like
// $titles = array("al-","dr","rev","phd","mr","ms","jr","sr","rabbi","fr","father","the","rev'd","prof");
public function is_salutation($word) {
// ignore periods
$word = str_replace('.','',strtolower($word));
// returns normalized values
if ($word == "mr" || $word == "master" || $word == "mister")
return "Mr.";
else if ($word == "mrs")
return "Mrs.";
else if ($word == "miss" || $word == "ms")
return "Ms.";
else if ($word == "dr")
return "Dr.";
else if ($word == "the")
return " ";
else if ($word == "rev" || $word == "rev'd" || $word == "reverend")
return "Rev.";
else if ($word == "fr" || $word == "father")
return "Fr.";
else if ($word == "sr" || $word == "sister")
return "Sr.";
else if ($word == "prof" || $word == "professor")
return "Prof.";
else
return false;
}
// detect and format common suffixes
public function is_suffix($word) {
// ignore periods
$word = str_replace('.','',$word);
// these are some common suffixes - what am I missing?
$suffix_array = array('I','II','III','IV','V','Senior','Junior','Jr','Sr','PhD','APR','RPh','PE','MD','MA','DMD','CME');
foreach ($suffix_array as $suffix) {
if (strtolower($suffix) == strtolower($word))
return $suffix;
}
return false;
}
// detect compound last names like "Von Fange"
public function is_compound_lname($word) {
$word = strtolower($word);
// these are some common prefixes that identify a compound last names - what am I missing?
$words = array('vere','von','van','de','del','della','di','da','pietro','vanden','du','st.','st','la','ter');
return array_search($word,$words);
}
// single letter, possibly followed by a period
public function is_initial($word) {
return ((strlen($word) == 1) || (strlen($word) == 2 && $word{1} == "."));
}
// detect mixed case words like "McDonald"
// returns false if the string is all one case
public function is_camel_case($word) {
if (preg_match("|[A-Z]+|s", $word) && preg_match("|[a-z]+|s", $word))
return true;
return false;
}
// ucfirst words split by dashes or periods
// ucfirst all upper/lower strings, but leave camelcase words alone
public function fix_case($word) {
// uppercase words split by dashes, like "Kimura-Fay"
$word = $this->safe_ucfirst("-",$word);
// uppercase words split by periods, like "J.P."
$word = $this->safe_ucfirst(".",$word);
return $word;
}
// helper public function for fix_case
public function safe_ucfirst($seperator, $word) {
// uppercase words split by the seperator (ex. dashes or periods)
$parts = explode($seperator,$word);
foreach ($parts as $word) {
$words[] = ($this->is_camel_case($word)) ? $word : ucfirst(strtolower($word));
}
return implode($seperator,$words);
}
}
?>