pnp_split_full_name

The Paid Memberships Pro pnp split full name function.

Description

pnp_split_full_name( (string) $full_name ); 

Parameters (1)

0. $full_name (string)
The full name.

Usage

  1. if ( !function_exists( 'pnp_split_full_name' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'paid-memberships-pro/includes/lib/name-parser.php'; 
  3.  
  4. // The full name. 
  5. $full_name = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = pnp_split_full_name($full_name); 
  9.  

Defined (1)

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

/includes/lib/name-parser.php  
  1. function pnp_split_full_name($full_name) { 
  2. if(empty($full_name)) 
  3. return ""; 
  4.  
  5. $fname = $lname = $initials = NULL; 
  6.  
  7. $full_name = trim($full_name); 
  8. // split into words 
  9. $unfiltered_name_parts = explode(" ", $full_name); 
  10. $name_parts = array(); 
  11. // completely ignore any words in parentheses 
  12. foreach ($unfiltered_name_parts as $word) { 
  13. if (!empty($word) && $word{0} != "(") 
  14. $name_parts[] = $word; 
  15. $num_words = sizeof($name_parts); 
  16.  
  17. // is the first word a title? (Mr. Mrs, etc) 
  18. $salutation = pnp_is_salutation($name_parts[0]); 
  19. $suffix = pnp_is_suffix($name_parts[sizeof($name_parts)-1]); 
  20.  
  21. //setthe range for the middle part of the name (trim prefixes & suffixes) 
  22. $start = ($salutation) ? 1 : 0; 
  23. $end = ($suffix) ? $num_words-1 : $num_words; 
  24.  
  25. // concat the first name 
  26. for ($i=$start; $i < $end-1; $i++) { 
  27. $word = $name_parts[$i]; 
  28. // move on to parsing the last name if we find an indicator of a compound last name (Von, Van, etc) 
  29. // we use $i != $start to allow for rare cases where an indicator is actually the first name (like "Von Fabella") 
  30. if (pnp_is_compound_lname($word) && $i != $start) 
  31. break; 
  32. // is it a middle initial or part of their first name? 
  33. // if we start off with an initial, we'll call it the first name 
  34. if (pnp_is_initial($word)) { 
  35. // is the initial the first word?  
  36. if ($i == $start) { 
  37. // if so, do a look-ahead to see if they go by their middle name 
  38. // for ex: "R. Jason Smith" => "Jason Smith" & "R." is stored as an initial 
  39. // but "R. J. Smith" => "R. Smith" and "J." is stored as an initial 
  40. if (pnp_is_initial($name_parts[$i+1])) 
  41. $fname .= " ".strtoupper($word); 
  42. else 
  43. $initials .= " ".strtoupper($word); 
  44. // otherwise, just go ahead and save the initial 
  45. } else { 
  46. $initials .= " ".strtoupper($word); 
  47. } else { 
  48. $fname .= " ".pnp_fix_case($word); 
  49. }  
  50.  
  51. // check that we have more than 1 word in our string 
  52. if ($end-$start > 1) { 
  53. // concat the last name 
  54. for ($i; $i < $end; $i++) { 
  55. $lname .= " ".pnp_fix_case($name_parts[$i]); 
  56. } else { 
  57. // otherwise, single word strings are assumed to be first names 
  58. $fname = pnp_fix_case($name_parts[$i]); 
  59.  
  60. // return the various parts in an array 
  61. $name['salutation'] = $salutation; 
  62. $name['fname'] = trim($fname); 
  63. $name['initials'] = trim($initials); 
  64. $name['lname'] = trim($lname); 
  65. $name['suffix'] = $suffix; 
  66. return $name;