php - Is str_replace with an array the right way to format a person's last name? -


out of mysql database have list of names like

  • smith
  • frank
  • dent md
  • smith sr.
  • jones, jr.
  • smith-jones
  • o'toole

i need list be

  • smith
  • frank
  • dent
  • smith
  • jones
  • smith-jones
  • otoole

by format mean want "main" part of last name eliminating

  • non-alphanumeric characters
  • spaces
  • titles (jr, sr, md, etc...)

i realize in cases "changing" person's name it's not being used in way see.

right doing like:

$toreplace  = array('.', ',', '-', ' jr', ' sr', ' md', ' do', "'", ' '); //for each result query     $lname = str_replace($toreplace, '', $row_rsgetusers['lname']);     $lname = strtolower($lname); 

then, after awhile name shows wright cisa have update $toreplace array account that. (i have no control on input of names)

is best way go doing or there better way/library out there should using eliminates need me manually update $toreplace array occasionally?

this should asked. though could've find here @ stack: replace characters except letters, numbers, spaces , underscores how remove after space in php?

<?php     $user_list = array('smith', 'frank', 'dent md', 'smith sr.', 'jones, jr.', 'smith-jones', "o'toole");     print_r($user_list);     $string = "o'toole";     $temp_array = array();     foreach($user_list $user_string){         //remove special chars , convert upper case letters lower case         $string = strtolower(preg_replace("/[^ \w]+/", "", $user_string));         //remove after space, case example: jones, jr.         $string = explode(' ', $string);         $string = $string[0];         //assign "standartized?" string          $temp_array[] = $string;     }     print_r($temp_array); ?>  array (     [0] => smith     [1] => frank     [2] => dent md     [3] => smith sr.     [4] => jones, jr.     [5] => smith-jones     [6] => o'toole ) array (     [0] => smith     [1] => frank     [2] => dent     [3] => smith     [4] => jones     [5] => smithjones     [6] => otoole ) 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -