我正在閱讀有一些名稱不一致的文本文件。比如我有這樣的名字:是否有更簡單的方法來修復名稱後綴?
MILLER, WALTER MCNAB IV
DUPONT, THOMAS B. II
HARDEWAY, GRANT U. SR.
正如你看到的,我可以通過姓氏和名字不過後綴放置不當分裂名。理想的名字應該是:
WALTER MCNAB MILLER IV
THOMAS B. DUPONT II
GRANT U. HARDEWAY SR.
這就是說,這是我有,但我覺得我的過於複雜的事情簡單。有小費嗎?
private function extractFullName($name){
//I cant do V since V could be someone's middle initial also :(
$suffix = collect(['II', 'III', 'IV', 'JR.', 'SR.']);
// lets first assemble a full name from the row
$fullNameArray = explode(",", $name);
$firstName=trim($fullNameArray[1]);
$lastName=trim($fullNameArray[0]);
//lets look at the FIRST NAME for II/III/IV/JR./SR.
$suffix = $suffix->each(function ($item, $key) use ($firstName, $lastName) {
if (strpos($firstName, " ".$item)){
$firstName = preg_replace('/'.$item.'$/', '', $firstName);
$lastName.=' '.$item;
return false;
}
});
//Basically arranging it in a 'firstname lastname II/III/IV/JR./SR.' format
$name = $firstName." ".$lastName);
$name = ucwords(strtolower($lawyerName));
return $name;
}
我正在投票結束這個問題,因爲它屬於http://codereview.stackexchange.com/,因爲你在問如何改進它,而不是解決問題。 –
http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ <製作關於名字的假設是在不穩固的地面上行走> https://en.wikipedia.org/wiki/ Prince_(音樂家) – CD001