姓名拆分爲姓氏和名字的方法[php版]

/** 
 * 把全名拆分爲姓氏和名字 
 * @param string $fullname 全名 
 * @return array 一維數組,元素一是姓,元素二爲名 
 * @author: 風柏楊<waitatlee@163.com> 
 */
function splitName($fullname){  
     $hyphenated = array('歐陽','太史','端木','上官','司馬','東方','獨孤','南宮','万俟','聞人','夏侯','諸葛','尉遲','公羊','赫連','澹臺','皇甫',  
        '宗政','濮陽','公冶','太叔','申屠','公孫','慕容','仲孫','鍾離','長孫','宇文','城池','司徒','鮮于','司空','汝嫣','閭丘','子車','亓官',  
        '司寇','巫馬','公西','顓孫','壤駟','公良','漆雕','樂正','宰父','穀梁','拓跋','夾谷','軒轅','令狐','段幹','百里','呼延','東郭','南門',  
        '羊舌','微生','公戶','公玉','公儀','梁丘','公仲','公上','公門','公山','公堅','左丘','公伯','西門','公祖','第五','公乘','貫丘','公皙',  
        '南榮','東里','東宮','仲長','子書','子桑','即墨','達奚','褚師');  
        $vLength = mb_strlen($fullname, 'utf-8');  
        $lastname = '';  
        $firstname = '';//前爲姓,後爲名  
        if($vLength > 2){  
            $preTwoWords = mb_substr($fullname, 0, 2, 'utf-8');//取命名的前兩個字,看是否在複姓庫中  
            if(in_array($preTwoWords, $hyphenated)){  
                $lastname = $preTwoWords;  
                $firstname = mb_substr($fullname, 2, 10, 'utf-8');  
            }else{  
                $lastname = mb_substr($fullname, 0, 1, 'utf-8');  
                $firstname = mb_substr($fullname, 1, 10, 'utf-8');  
            }  
        }else if($vLength == 2){//全名只有兩個字時,之前一個爲姓,後一下爲名  
            $lastname = mb_substr($fullname ,0, 1, 'utf-8');  
            $firstname = mb_substr($fullname, 1, 10, 'utf-8');  
        }else{  
            $lastname = $fullname;  
        }  
        return array($lastname, $firstname);  
}

其中,$hyphenated這個數組存的是複姓庫。php

相關文章
相關標籤/搜索