ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilFullnameParser.php
Go to the documentation of this file.
1 <?php
12 {
13 
18  private $titles;
19 
24  private $prefices;
25 
30  private $suffices;
31 
36  private $title;
37 
42  private $first;
43 
48  private $middle;
49 
54  private $last;
55 
60  private $suffix;
61 
66  private $fullName;
67 
72  private $notParseable;
73 
74 
82  public function __construct($initString = "")
83  {
84  $this->title = "";
85  $this->first = "";
86  $this->middle = "";
87  $this->last = "";
88  $this->suffix = "";
89 
90  //$this->titles = array('dr','doctor','miss','misses','mr','mister','mrs','ms','judge','sir','madam','madame');
91 
92  // added Military Titles
93  $this->titles = array('dr','doctor','miss','misses','mr','mister','mrs','ms','judge','sir','madam','madame','AB','2ndLt','Amn','1stLt','A1C','Capt','SrA','Maj','SSgt','LtCol','TSgt','Col','BrigGen','1stSgt','MajGen','SMSgt','LtGen','1stSgt','Gen','CMSgt','1stSgt','CCMSgt','CMSAF','PVT','2LT','PV2','1LT','PFC','CPT','SPC','MAJ','CPL','LTC','SGT','COL','SSG','BG','SFC','MG','MSG','LTG','1SGT','GEN','SGM','CSM','SMA','WO1','WO2','WO3','WO4','WO5','ENS','SA','LTJG','SN','LT','PO3','LCDR','PO2','CDR','PO1','CAPT','CPO','RADM(LH)','SCPO','RADM(UH)','MCPO','VADM','MCPOC','ADM','MPCO-CG','CWO-2','CWO-3','CWO-4','Pvt','2ndLt','PFC','1stLt','LCpl','Capt','Cpl','Maj','Sgt','LtCol','SSgt','Col','GySgt','BGen','MSgt','MajGen','1stSgt','LtGen','MGySgt','Gen','SgtMaj','SgtMajMC','WO-1','CWO-2','CWO-3','CWO-4','CWO-5','ENS','SA','LTJG','SN','LT','PO3','LCDR','PO2','CDR','PO1','CAPT','CPO','RDML','SCPO','RADM','MCPO','VADM','MCPON','ADM','FADM','WO1','CWO2','CWO3','CWO4','CWO5');
94 
95  $this->prefices = array('bon','ben','bin','da','dal','de','del','der','de','e','la','le','san','st','ste','van','vel','von');
96  $this->suffices = array('esq','esquire','jr','sr','2','i','ii','iii','iv','v','clu','chfc','cfp','md','phd');
97  $this->fullName = "";
98  $this->notParseable = false;
99 
100  // if initialized by value, set class variable and then parse
101  if ($initString != "") {
102  $this->fullName = $initString;
103  $this->parse();
104  }
105  }
106 
107 
112  public function __destruct()
113  {
114  }
115 
116 
117 
122  public function getFirstName()
123  {
124  return $this->first;
125  }
126 
127 
128 
133  public function getMiddleName()
134  {
135  return $this->middle;
136  }
137 
138 
139 
144  public function getLastName()
145  {
146  return $this->last;
147  }
148 
149 
150 
155  public function getTitle()
156  {
157  return $this->title;
158  }
159 
160 
161 
166  public function getSuffix()
167  {
168  return $this->suffix;
169  }
170 
171 
172 
177  public function getNotParseable()
178  {
179  return $this->notParseable;
180  }
181 
182 
183 
189  public function setFullName($newFullName)
190  {
191  $this->fullName = $newFullName;
192  }
193 
194 
195 
203  private function inArrayNorm($needle, $haystack)
204  {
205  $needle = trim(strtolower(str_replace('.', '', $needle)));
206  return in_array($needle, $haystack);
207  }
208 
209 
210 
216  public function parse()
217  {
218  // reset values
219  $this->title = "";
220  $this->first = "";
221  $this->middle = "";
222  $this->last = "";
223  $this->suffix = "";
224  $this->notParseable = false;
225 
226  // break up name based on number of commas
227  $pieces = explode(',', preg_replace('/\s+/', ' ', trim($this->fullName)));
228  $numPieces = count($pieces);
229 
230  switch ($numPieces) {
231 
232  // array(title first middle last suffix)
233  case 1:
234  $subPieces = explode(' ', trim($pieces[0]));
235  $numSubPieces = count($subPieces);
236  for ($i = 0; $i < $numSubPieces; $i++) {
237  $current = trim($subPieces[$i]);
238  if ($i < ($numSubPieces - 1)) {
239  $next = trim($subPieces[$i + 1]);
240  } else {
241  $next = "";
242  }
243  if ($i == 0 && $this->inArrayNorm($current, $this->titles)) {
244  $this->title = $current;
245  continue;
246  }
247  if ($this->first == "") {
248  $this->first = $current;
249  continue;
250  }
251  if ($i == $numSubPieces - 2 && ($next != "") && $this->inArrayNorm($next, $this->suffices)) {
252  if ($this->last != "") {
253  $this->last .= " " . $current;
254  } else {
255  $this->last = $current;
256  }
257  $this->suffix = $next;
258  break;
259  }
260  if ($i == $numSubPieces - 1) {
261  if ($this->last != "") {
262  $this->last .= " " . $current;
263  } else {
264  $this->last = $current;
265  }
266  continue;
267  }
268  if ($this->inArrayNorm($current, $this->prefices)) {
269  if ($this->last != "") {
270  $this->last .= " " . $current;
271  } else {
272  $this->last = $current;
273  }
274  continue;
275  }
276  if ($next == 'y' || $next == 'Y') {
277  if ($this->last != "") {
278  $this->last .= " " . $current;
279  } else {
280  $this->last = $current;
281  }
282  continue;
283  }
284  if ($this->last != "") {
285  $this->last .= " " . $current;
286  continue;
287  }
288  if ($this->middle != "") {
289  $this->middle .= " " . $current;
290  } else {
291  $this->middle = $current;
292  }
293  }
294  break;
295 
296  default:
297  switch ($this->inArrayNorm($pieces[1], $this->suffices)) {
298 
299  // array(title first middle last, suffix [, suffix])
300  case true:
301  $subPieces = explode(' ', trim($pieces[0]));
302  $numSubPieces = count($subPieces);
303  for ($i = 0; $i < $numSubPieces; $i++) {
304  $current = trim($subPieces[$i]);
305  if ($i < ($numSubPieces - 1)) {
306  $next = trim($subPieces[$i + 1]);
307  } else {
308  $next = "";
309  }
310  if ($i == 0 && $this->inArrayNorm($current, $this->titles)) {
311  $this->title = $current;
312  continue;
313  }
314  if ($this->first == "") {
315  $this->first = $current;
316  continue;
317  }
318  if ($i == $numSubPieces - 1) {
319  if ($this->last != "") {
320  $this->last .= " " . $current;
321  } else {
322  $this->last = $current;
323  }
324  continue;
325  }
326  if ($this->inArrayNorm($current, $this->prefices)) {
327  if ($this->last != "") {
328  $this->last .= " " . $current;
329  } else {
330  $this->last = $current;
331  }
332  continue;
333  }
334  if ($next == 'y' || $next == 'Y') {
335  if ($this->last != "") {
336  $this->last .= " " . $current;
337  } else {
338  $this->last = $current;
339  }
340  continue;
341  }
342  if ($this->last != "") {
343  $this->last .= " " . $current;
344  continue;
345  }
346  if ($this->middle != "") {
347  $this->middle .= " " . $current;
348  } else {
349  $this->middle = $current;
350  }
351  }
352  $this->suffix = trim($pieces[1]);
353  for ($i = 2; $i < $numPieces; $i++) {
354  $this->suffix .= ", " . trim($pieces[$i]);
355  }
356  break;
357 
358  // array(last, title first middles[,] suffix [,suffix])
359  case false:
360  $subPieces = explode(' ', trim($pieces[1]));
361  $numSubPieces = count($subPieces);
362  for ($i = 0; $i < $numSubPieces; $i++) {
363  $current = trim($subPieces[$i]);
364  if ($i < ($numSubPieces - 1)) {
365  $next = trim($subPieces[$i + 1]);
366  } else {
367  $next = "";
368  }
369  if ($i == 0 && $this->inArrayNorm($current, $this->titles)) {
370  $this->title = $current;
371  continue;
372  }
373  if ($this->first == "") {
374  $this->first = $current;
375  continue;
376  }
377  if ($i == $numSubPieces - 2 && ($next != "") && $this->inArrayNorm($next, $this->suffices)) {
378  if ($this->middle != "") {
379  $this->middle .= " " . $current;
380  } else {
381  $this->middle = $current;
382  }
383  $this->suffix = $next;
384  break;
385  }
386  if ($i == $numSubPieces - 1 && $this->inArrayNorm($current, $this->suffices)) {
387  $this->suffix = $current;
388  continue;
389  }
390  if ($this->middle != "") {
391  $this->middle .= " " . $current;
392  } else {
393  $this->middle = $current;
394  }
395  }
396  if (isset($pieces[2]) && $pieces[2]) {
397  if ($this->last == "") {
398  $this->suffix = trim($pieces[2]);
399  for ($s = 3; $s < $numPieces; $s++) {
400  $this->suffix .= ", " . trim($pieces[$s]);
401  }
402  } else {
403  for ($s = 2; $s < $numPieces; $s++) {
404  $this->suffix .= ", " . trim($pieces[$s]);
405  }
406  }
407  }
408  $this->last = $pieces[0];
409  break;
410  }
411  unset($pieces);
412  break;
413  }
414  if ($this->first == "" && $this->middle == "" && $this->last == "") {
415  $this->notParseable = true;
416  }
417  }
418 }
inArrayNorm($needle, $haystack)
Determine if the needle is in the haystack.
getMiddleName()
Access Method public.
$s
Definition: pwgen.php:45
getSuffix()
Access Method public.
getFirstName()
Access Method public.
parse()
Extract the elements of the full name into separate parts.
__destruct()
Destructor public.
__construct($initString="")
Constructor: Setup the object, initialise the variables, and if instantiated with a name - parse it a...
getNotParseable()
Access Method public.
$i
Definition: disco.tpl.php:19
getTitle()
Access Method public.
getLastName()
Access Method public.
setFullName($newFullName)
Mutator Method public.