PHP实现姓名根据首字母排序的类与方法(实例代码)

 3511

这篇文章主要介绍了php实现姓名根据首字母排序的类与方法,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧。

php将名字按首字母进行排序,具体代码如下所示:

  1. <?php 
  2. public function getFirstChar($s){ 
  3.     $s0 = mb_substr($s,0,3); //获取名字的姓 
  4.     $s = iconv('UTF-8','gb2312', $s0); //将UTF-8转换成GB2312编码 
  5.     //dump($s0); 
  6.     if (ord($s0)>128) { 
  7.         //汉字开头,汉字没有以U、V开头的 
  8.         $asc=ord($s{0})*256+ord($s{1})-65536; 
  9.         if($asc>=-20319 and $asc<=-20284)return "A"; 
  10.         if($asc>=-20283 and $asc<=-19776)return "B"; 
  11.         if($asc>=-19775 and $asc<=-19219)return "C"; 
  12.         if($asc>=-19218 and $asc<=-18711)return "D"; 
  13.         if($asc>=-18710 and $asc<=-18527)return "E"; 
  14.         if($asc>=-18526 and $asc<=-18240)return "F"; 
  15.         if($asc>=-18239 and $asc<=-17760)return "G"; 
  16.         if($asc>=-17759 and $asc<=-17248)return "H"; 
  17.         if($asc>=-17247 and $asc<=-17418)return "I"; 
  18.         if($asc>=-17417 and $asc<=-16475)return "J"; 
  19.         if($asc>=-16474 and $asc<=-16213)return "K"; 
  20.         if($asc>=-16212 and $asc<=-15641)return "L"; 
  21.         if($asc>=-15640 and $asc<=-15166)return "M"; 
  22.         if($asc>=-15165 and $asc<=-14923)return "N"; 
  23.         if($asc>=-14922 and $asc<=-14915)return "O"; 
  24.         if($asc>=-14914 and $asc<=-14631)return "P"; 
  25.         if($asc>=-14630 and $asc<=-14150)return "Q"; 
  26.         if($asc>=-14149 and $asc<=-14091)return "R"; 
  27.         if($asc>=-14090 and $asc<=-13319)return "S"; 
  28.         if($asc>=-13318 and $asc<=-12839)return "T"; 
  29.         if($asc>=-12838 and $asc<=-12557)return "W"; 
  30.         if($asc>=-12556 and $asc<=-11848)return "X"; 
  31.         if($asc>=-11847 and $asc<=-11056)return "Y"; 
  32.         if($asc>=-11055 and $asc<=-10247)return "Z"; 
  33.     } else if(ord($s)>=48 and ord($s)<=57) { 
  34.         //数字开头 
  35.         switch(iconv_substr($s,0,1,'utf-8')){ 
  36.             case 1:return "Y"; 
  37.             case 2:return "E"; 
  38.             case 3:return "S"; 
  39.             case 4:return "S"; 
  40.             case 5:return "W"; 
  41.             case 6:return "L"; 
  42.             case 7:return "Q"; 
  43.             case 8:return "B"; 
  44.             case 9:return "J"; 
  45.             case 0:return "L"; 
  46.         } 
  47.     } else if(ord($s)>=65 and ord($s)<=90) { //大写英文开头 
  48.         return substr($s,0,1); 
  49.     } else if(ord($s)>=97 and ord($s)<=122) { //小写英文开头 
  50.         return strtoupper(substr($s,0,1)); 
  51.     } else { 
  52.         return iconv_substr($s0,0,1,'utf-8'); 
  53.         //中英混合的词语,不适合上面的各种情况,因此直接提取首个字符即可 
  54.     } 
  55. } 
  56. public function addPeople() 
  57. { 
  58.     $userName = array('张三','马大帅','李四','王五','小二','猫蛋','狗蛋','王花','三毛','小明','李刚','张飞'); 
  59.     sort($userName); 
  60.     foreach($userName as $name){ 
  61.         $char = $this->getFirstChar($name); 
  62.         $nameArray = array(); 
  63.         if(count($charArray[$char])!=0){ 
  64.             $nameArray = $charArray[$char]; 
  65.         } 
  66.         array_push($nameArray,$name); 
  67.         $charArray[$char] = $nameArray; 
  68.     } 
  69.     ksort($charArray); 
  70.     $this->assign('_list',$charArray); 
  71. }

页面显示

  1. <foreach name="_list" item="vo" key="k"> 
  2.     <p>{$k}</p> 
  3.     <foreach name="vo" item="l"> 
  4.         <span class="name">{$l}</span> 
  5.     </foreach> 
  6. </foreach>


本文网址:https://www.zztuku.com/index.php/detail-10322.html
站长图库 - PHP实现姓名根据首字母排序的类与方法(实例代码)
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐

    白色线条图标矢量素材