ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
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  $this->title = "";
84  $this->first = "";
85  $this->middle = "";
86  $this->last = "";
87  $this->suffix = "";
88 
89  //$this->titles = array('dr','doctor','miss','misses','mr','mister','mrs','ms','judge','sir','madam','madame');
90 
91  // added Military Titles
92  $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');
93 
94  $this->prefices = array('bon','ben','bin','da','dal','de','del','der','de','e','la','le','san','st','ste','van','vel','von');
95  $this->suffices = array('esq','esquire','jr','sr','2','i','ii','iii','iv','v','clu','chfc','cfp','md','phd');
96  $this->fullName = "";
97  $this->notParseable = FALSE;
98 
99  // if initialized by value, set class variable and then parse
100  if ( $initString != "" ) {
101  $this->fullName = $initString;
102  $this->parse();
103  }
104  }
105 
106 
111  public function __destruct() {}
112 
113 
114 
119  public function getFirstName() { return $this->first; }
120 
121 
122 
127  public function getMiddleName() { return $this->middle; }
128 
129 
130 
135  public function getLastName() { return $this->last; }
136 
137 
138 
143  public function getTitle() { return $this->title; }
144 
145 
146 
151  public function getSuffix() { return $this->suffix; }
152 
153 
154 
159  public function getNotParseable() { return $this->notParseable; }
160 
161 
162 
168  public function setFullName( $newFullName ) { $this->fullName = $newFullName; }
169 
170 
171 
179  private function inArrayNorm( $needle, $haystack ) {
180  $needle = trim( strtolower( str_replace( '.', '', $needle ) ) );
181  return in_array( $needle, $haystack );
182  }
183 
184 
185 
191  public function parse() {
192  // reset values
193  $this->title = "";
194  $this->first = "";
195  $this->middle = "";
196  $this->last = "";
197  $this->suffix = "";
198  $this->notParseable = FALSE;
199 
200  // break up name based on number of commas
201  $pieces = explode( ',', preg_replace('/\s+/', ' ', trim( $this->fullName ) ) );
202  $numPieces = count( $pieces );
203 
204  switch ( $numPieces ) {
205 
206  // array(title first middle last suffix)
207  case 1:
208  $subPieces = explode(' ', trim( $pieces[0] ) );
209  $numSubPieces = count( $subPieces );
210  for ( $i = 0; $i < $numSubPieces; $i++ ) {
211  $current = trim( $subPieces[$i] );
212  if ( $i < ($numSubPieces-1) ) {
213  $next = trim( $subPieces[$i+1] );
214  } else {
215  $next = "";
216  }
217  if ( $i == 0 && $this->inArrayNorm( $current, $this->titles ) ) {
218  $this->title = $current;
219  continue;
220  }
221  if ( $this->first == "" ) {
222  $this->first = $current;
223  continue;
224  }
225  if ( $i == $numSubPieces-2 && ($next != "") && $this->inArrayNorm( $next, $this->suffices ) ) {
226  if ( $this->last != "") {
227  $this->last .= " ".$current;
228  } else {
229  $this->last = $current;
230  }
231  $this->suffix = $next;
232  break;
233  }
234  if ( $i == $numSubPieces-1 ) {
235  if ( $this->last != "" ) {
236  $this->last .= " ".$current;
237  } else {
238  $this->last = $current;
239  }
240  continue;
241  }
242  if ( $this->inArrayNorm( $current, $this->prefices ) ) {
243  if ( $this->last != "" ) {
244  $this->last .= " ".$current;
245  } else {
246  $this->last = $current;
247  }
248  continue;
249  }
250  if ( $next == 'y' || $next == 'Y' ) {
251  if ( $this->last != "" ) {
252  $this->last .= " ".$current;
253  } else {
254  $this->last = $current;
255  }
256  continue;
257  }
258  if ( $this->last != "" ) {
259  $this->last .= " ".$current;
260  continue;
261  }
262  if( $this->middle != "" ) {
263  $this->middle .= " ".$current;
264  } else {
265  $this->middle = $current;
266  }
267  }
268  break;
269 
270  default:
271  switch( $this->inArrayNorm( $pieces[1], $this->suffices ) ) {
272 
273  // array(title first middle last, suffix [, suffix])
274  case TRUE:
275  $subPieces = explode(' ', trim( $pieces[0] ) );
276  $numSubPieces = count( $subPieces );
277  for ( $i = 0; $i < $numSubPieces; $i++ ) {
278  $current = trim( $subPieces[$i] );
279  if ( $i < ($numSubPieces-1) ) {
280  $next = trim( $subPieces[$i+1] );
281  } else {
282  $next = "";
283  }
284  if ( $i == 0 && $this->inArrayNorm( $current, $this->titles ) ) {
285  $this->title = $current;
286  continue;
287  }
288  if ( $this->first == "" ) {
289  $this->first = $current;
290  continue;
291  }
292  if ( $i == $numSubPieces-1 ) {
293  if ( $this->last != "" ) {
294  $this->last .= " ".$current;
295  } else {
296  $this->last = $current;
297  }
298  continue;
299  }
300  if ( $this->inArrayNorm( $current, $this->prefices ) ) {
301  if ( $this->last != "" ) {
302  $this->last .= " ".$current;
303  } else {
304  $this->last = $current;
305  }
306  continue;
307  }
308  if ( $next == 'y' || $next == 'Y' ) {
309  if ( $this->last != "" ) {
310  $this->last .= " ".$current;
311  } else {
312  $this->last = $current;
313  }
314  continue;
315  }
316  if ( $this->last != "" ) {
317  $this->last .= " ".$current;
318  continue;
319  }
320  if ( $this->middle != "" ) {
321  $this->middle .= " ".$current;
322  } else {
323  $this->middle = $current;
324  }
325  }
326  $this->suffix = trim($pieces[1]);
327  for ( $i = 2; $i < $numPieces; $i++ ) {
328  $this->suffix .= ", ". trim( $pieces[$i] );
329  }
330  break;
331 
332  // array(last, title first middles[,] suffix [,suffix])
333  case FALSE:
334  $subPieces = explode( ' ', trim( $pieces[1] ) );
335  $numSubPieces = count( $subPieces );
336  for ( $i = 0; $i < $numSubPieces; $i++ ) {
337  $current = trim( $subPieces[$i] );
338  if ( $i < ($numSubPieces-1) ) {
339  $next = trim( $subPieces[$i+1] );
340  } else {
341  $next = "";
342  }
343  if ( $i == 0 && $this->inArrayNorm( $current, $this->titles ) ) {
344  $this->title = $current;
345  continue;
346  }
347  if ( $this->first == "" ) {
348  $this->first = $current;
349  continue;
350  }
351  if ( $i == $numSubPieces-2 && ($next != "") && $this->inArrayNorm( $next, $this->suffices ) ) {
352  if ( $this->middle != "" ) {
353  $this->middle .= " ".$current;
354  } else {
355  $this->middle = $current;
356  }
357  $this->suffix = $next;
358  break;
359  }
360  if ( $i == $numSubPieces-1 && $this->inArrayNorm( $current, $this->suffices ) ) {
361  $this->suffix = $current;
362  continue;
363  }
364  if ( $this->middle != "" ) {
365  $this->middle .= " ".$current;
366  } else {
367  $this->middle = $current;
368  }
369  }
370  if( isset($pieces[2]) && $pieces[2] ) {
371  if ( $this->last == "" ) {
372  $this->suffix = trim( $pieces[2] );
373  for ($s = 3; $s < $numPieces; $s++) {
374  $this->suffix .= ", ". trim( $pieces[$s] );
375  }
376  } else {
377  for ($s = 2; $s < $numPieces; $s++) {
378  $this->suffix .= ", ". trim( $pieces[$s] );
379  }
380  }
381  }
382  $this->last = $pieces[0];
383  break;
384  }
385  unset( $pieces );
386  break;
387  }
388  if ( $this->first == "" && $this->middle == "" && $this->last == "" ) {
389  $this->notParseable = TRUE;
390  }
391  }
392 }