php将名字按首字母进行排序

<?php

public function getFirstChar($s){
$s0 = mb_substr($s,0,3); //获取名字的姓
$s = iconv('UTF-8','gb2312'$s0); //将UTF-8转换成GB2312编码
//dump($s0);
if (ord($s0)>128) { //汉字开头,汉字没有以U、V开头的
$asc=ord($s{0})*256+ord($s{1})-65536;
if($asc>=-20319 and $asc<=-20284)return "A";
if($asc>=-20283 and $asc<=-19776)return "B";
if($asc>=-19775 and $asc<=-19219)return "C";
if($asc>=-19218 and $asc<=-18711)return "D";
if($asc>=-18710 and $asc<=-18527)return "E";
if($asc>=-18526 and $asc<=-18240)return "F";
if($asc>=-18239 and $asc<=-17760)return "G";
if($asc>=-17759 and $asc<=-17248)return "H";
if($asc>=-17247 and $asc<=-17418)return "I";
if($asc>=-17417 and $asc<=-16475)return "J";
if($asc>=-16474 and $asc<=-16213)return "K";
if($asc>=-16212 and $asc<=-15641)return "L";
if($asc>=-15640 and $asc<=-15166)return "M";
if($asc>=-15165 and $asc<=-14923)return "N";
if($asc>=-14922 and $asc<=-14915)return "O";
if($asc>=-14914 and $asc<=-14631)return "P";
if($asc>=-14630 and $asc<=-14150)return "Q";
if($asc>=-14149 and $asc<=-14091)return "R";
if($asc>=-14090 and $asc<=-13319)return "S";
if($asc>=-13318 and $asc<=-12839)return "T";
if($asc>=-12838 and $asc<=-12557)return "W";
if($asc>=-12556 and $asc<=-11848)return "X";
if($asc>=-11847 and $asc<=-11056)return "Y";
if($asc>=-11055 and $asc<=-10247)return "Z";
}else if(ord($s)>=48 and ord($s)<=57){ //数字开头
switch(iconv_substr($s,0,1,'utf-8')){
case 1:return "Y";
case 2:return "E";
case 3:return "S";
case 4:return "S";
case 5:return "W";
case 6:return "L";
case 7:return "Q";
case 8:return "B";
case 9:return "J";
case 0:return "L";
}
}else if(ord($s)>=65 and ord($s)<=90){ //大写英文开头
return substr($s,0,1);
}else if(ord($s)>=97 and ord($s)<=122){ //小写英文开头
return strtoupper(substr($s,0,1));
}
else
{
return iconv_substr($s0,0,1,'utf-8');
//中英混合的词语,不适合上面的各种情况,因此直接提取首个字符即可
}
}
public function addPeople()
{
$userName array('张三','马大帅','李四','王五','小二','猫蛋','狗蛋','王花','三毛','小明','李刚','张飞');
sort($userName);
foreach($userName as $name){
$char $this->getFirstChar($name);
$nameArray array();
if(count($charArray[$char])!=0){
$nameArray $charArray[$char];
}
array_push($nameArray,$name);
$charArray[$char] = $nameArray;
}
ksort($charArray);
$this->assign('_list',$charArray);
}
 
页面显示

<foreach name="_list" item="vo" key="k">

<p>{$k}</p>
<foreach name="vo" item="l">
<span class="name">{$l}</span>
</foreach>
</foreach>

php实现姓名按首字母排序的类与方法的更多相关文章

  1. 联系人的侧边字母索引ListView 将手机通讯录姓名通过首字母排序。

      package com.lixu.letterlistview; import java.util.ArrayList; import java.util.List; import org.apa ...

  2. Oracle 10g如何对用户姓名,按首字母排序、查询

    首先介绍Oracle 9i新增加的一个系统自带的排序函数  1.按首字母排序  在oracle9i中新增了按照拼音.部首.笔画排序功能.设置NLS_SORT值      SCHINESE_RADICA ...

  3. [转]Oracle 10g如何对用户姓名,按首字母排序、查询

    首先介绍Oracle 9i新增加的一个系统自带的排序函数  1.按首字母排序  在oracle9i中新增了按照拼音.部首.笔画排序功能.设置NLS_SORT值      SCHINESE_RADICA ...

  4. 获取手机通讯录放入PinnedSectionListView中,按名字首字母排序,并且实现拨打电话功能。

    package com.lixu.tongxunlu; import java.util.ArrayList; import com.lixu.tongxunlu.PinnedSectionListV ...

  5. php按照中文首字母排序

    1> 网络上很多php的工具类可以将汉字转为拼音: 2> 将拼音进行排序即可 另一种则是类似mysql转码方式: 1 foreach ($array as $key=>$value) ...

  6. HashMap加入数据后,会自动根据首字母排序

    1.Map<String, ArrayList<XX>> entityHashMap = new HashMap<>(); 然后增加一些数据,会发现根据String ...

  7. mysql中文名字按首字母排序

    在mysql数据库中可以使用GBK编码对中文进行排序,如名字按首字母排序 order by convert(substr(tu.username,1,1) using 'GBK') 其中substr方 ...

  8. mysql排序的中文首字母排序和自定义排序

    select * FROM organ_new where city_code = 'SZ0755' and organ_type = 'H' and state = '1' ORDER BY FIE ...

  9. mysql、oracle 中按照拼音首字母排序

    mysql中按照拼音首字母排序 convert(name using gbk) ASC 注:name 为字段名称 oracle中按照拼音首字母排序 nlssort(enterprise_name,'N ...

随机推荐

  1. ActiveMQ5.8.0安装及启动

    一.下载 官网地址:http://activemq.apache.org/download.html Windows版本:apache-activemq-5.8.0-bin.zip Linux版本:a ...

  2. models的单表操作

    orm创建数据库 from django.db import models # Create your models here. class Book(models.Model): name = mo ...

  3. 三、python小功能记录——杀掉进程

    import os os.system("taskkill /F /IM python.exe")#旧版 os.system("taskkill /F /IM py.ex ...

  4. [翻译] ZLSwipeableView

    ZLSwipeableView A simple view for building card like interface like Tinder and Potluck. ZLSwipeableV ...

  5. [翻译] DoActionSheet

    DoActionSheet https://github.com/donobono/DoActionSheet An replacement for UIActionSheet : block-bas ...

  6. In-app Billing 概述

    这个文档讲解了In-app Billing的基础构成和功能,为了能够把In-app Billing功能添加到你的程序中,你需要很好的理解这些内容. 注意: 保证你的应用遵从发布地国家的相关法律. 举例 ...

  7. Java实例---简单的数据库操作

    源码分析 DAOFactory.java package cn.ftl.mysql ; public class DAOFactory { public static IEmpDAO getIEmpD ...

  8. HTML学习---HTML状态码

    301 Moved Permanently 永久移动.请求的资源已被永久的移动到新URI,返回信息会包括新的URI,浏览器会自动定向到新URI.今后任何新的请求都应使用新的URI代替302 Found ...

  9. 彻底关闭Firefox自动更新的方法

    自己最近在使用firefox进行自动化测试时,发现配置好的firefox总是会自动更新,主要是因为一些高版本的浏览器无法安装firepath,没有firepath的火狐浏览器使用起来总是有很多的不方便 ...

  10. ES6重点--笔记(转)

    最常用的ES6特性 let, const, class, extends, super, arrow functions, template string, destructuring, defaul ...