FullNameParser

The PayPal for WooCommerce FullNameParser class.

Defined (1)

The class is defined in the following location(s).

/classes/lib/NameParser.php  
  1. class FullNameParser 
  2.  
  3. public function split_full_name($full_name) { 
  4. $full_name = trim($full_name); 
  5. // setup default values 
  6. $lname = ''; 
  7. // split into words 
  8. $unfiltered_name_parts = explode(" ", $full_name); 
  9. // completely ignore any words in parentheses 
  10. foreach ($unfiltered_name_parts as $word) { 
  11. if (@$word[0] != "(") 
  12. $name_parts[] = $word; 
  13. $num_words = sizeof($name_parts); 
  14.  
  15. // is the first word a title? (Mr. Mrs, etc) 
  16. $salutation = $this->is_salutation($name_parts[0]); 
  17.  
  18. // set the range for the middle part of the name (trim prefixes & suffixes) 
  19. $start = ($salutation) ? 1 : 0; 
  20.  
  21. //the first name 
  22. $fname = $this->fix_case($name_parts[$start]); 
  23.  
  24. // concat the last name 
  25. for ($i=$start+1; $i < $num_words; $i++) { 
  26. $lname .= " ".$this->fix_case($name_parts[$i]); 
  27.  
  28.  
  29. // return the various parts in an array 
  30. $name['fullname'] = $full_name; 
  31. $name['salutation'] = $salutation; 
  32. $name['fname'] = trim($fname); 
  33. $name['lname'] = trim($lname); 
  34. return $name; 
  35.  
  36. // detect and format standard salutations 
  37. // I'm only considering english honorifics for now & not words like 
  38. public function is_salutation($word) { 
  39. // ignore periods 
  40. $word = str_replace('.', '', strtolower($word)); 
  41. // returns normalized values 
  42. if ($word == "mr" || $word == "master" || $word == "mister") 
  43. return "Mr."; 
  44. else if ($word == "mrs") 
  45. return "Mrs."; 
  46. else if ($word == "miss" || $word == "ms") 
  47. return "Ms."; 
  48. else if ($word == "dr") 
  49. return "Dr."; 
  50. else if ($word == "rev") 
  51. return "Rev."; 
  52. else if ($word == "fr") 
  53. return "Fr."; 
  54. else 
  55. return false; 
  56.  
  57. // detect mixed case words like "McDonald" 
  58. // returns false if the string is all one case 
  59. public function is_camel_case($word) { 
  60. if (preg_match("|[A-Z]+|s", $word) && preg_match("|[a-z]+|s", $word)) 
  61. return true; 
  62. return false; 
  63.  
  64. // ucfirst words split by dashes or periods 
  65. // ucfirst all upper/lower strings, but leave camelcase words alone 
  66. public function fix_case($word) { 
  67. // uppercase words split by dashes, like "Kimura-Fay" 
  68. $word = $this->safe_ucfirst("-", $word); 
  69. // uppercase words split by periods, like "J.P." 
  70. $word = $this->safe_ucfirst(".", $word); 
  71. return $word; 
  72.  
  73. // helper public function for fix_case 
  74. public function safe_ucfirst($seperator, $word) { 
  75. // uppercase words split by the seperator (ex. dashes or periods) 
  76. $parts = explode($seperator, $word); 
  77. foreach ($parts as $word) { 
  78. $words[] = ($this->is_camel_case($word)) ? $word : ucfirst(strtolower($word)); 
  79. return implode($seperator, $words); 
  80.