Home > php教程 > php手册 > body text

姓名分离函数

WBOY
Release: 2016-06-07 11:42:17
Original
1599 people have browsed it

输入姓名将姓和名分离
<?php <br /> /**<br>  * 把全名拆分为姓氏和名字<br>  * @param string $fullname 全名<br>  * @return array 一维数组,元素一是姓,元素二为名<br>  */<br> function xingshi($fullname){<br>      $complex= array('欧阳','太史','端木','上官','司马','东方','独孤','南宫','万俟','闻人','夏侯','诸葛','尉迟','公羊','赫连','澹台','皇甫',<br>         '宗政','濮阳','公冶','太叔','申屠','公孙','慕容','仲孙','钟离','长孙','宇文','城池','司徒','鲜于','司空','汝嫣','闾丘','子车','亓官',<br>         '司寇','巫马','公西','颛孙','壤驷','公良','漆雕','乐正','宰父','谷梁','拓跋','夹谷','轩辕','令狐','段干','百里','呼延','东郭','南门',<br>         '羊舌','微生','公户','公玉','公仪','梁丘','公仲','公上','公门','公山','公坚','左丘','公伯','西门','公祖','第五','公乘','贯丘','公皙',<br>         '南荣','东里','东宫','仲长','子书','子桑','即墨','达奚','褚师');<br>         $vLength = mb_strlen($fullname, 'gbk');<br>         $lastname = '';<br>         $firstname = '';<br>         if($vLength > 2){<br>             $preTwoWords = mb_substr($fullname, 0, 2, 'gbk');<br>              <br>             if(in_array($preTwoWords, $complex)){<br>                 $lastname = $preTwoWords;<br>                 $firstname = mb_substr($fullname, 2, 10, 'gbk');<br>             }else{<br>                 $lastname = mb_substr($fullname, 0, 1, 'gbk');<br>                 $firstname = mb_substr($fullname, 1, 10, 'gbk');<br>             }<br>         }else if($vLength == 2){<br>             $lastname = mb_substr($fullname ,0, 1, 'gbk');<br>             $firstname = mb_substr($fullname, 1, 10, 'gbk');<br>         }else{<br>             $lastname = $fullname;<br>         }<br>         return array($lastname, $firstname);<br> }<br> $b="诸葛孔明";<br> $a=xingshi($b);<br> echo "姓氏:$a[0]";<br> echo "<br>";<br> echo "名字:$a[1]";<br> echo "<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&quot;;&lt;br&gt; print_r($a);exit;&lt;br&gt; ?&gt;</pre><div class="contentsignin">Copy after login</div></div>

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!