php实现姓名按首字母排序的类与方法
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实现姓名按首字母排序的类与方法的更多相关文章
- 联系人的侧边字母索引ListView 将手机通讯录姓名通过首字母排序。
package com.lixu.letterlistview; import java.util.ArrayList; import java.util.List; import org.apa ...
- Oracle 10g如何对用户姓名,按首字母排序、查询
首先介绍Oracle 9i新增加的一个系统自带的排序函数 1.按首字母排序 在oracle9i中新增了按照拼音.部首.笔画排序功能.设置NLS_SORT值 SCHINESE_RADICA ...
- [转]Oracle 10g如何对用户姓名,按首字母排序、查询
首先介绍Oracle 9i新增加的一个系统自带的排序函数 1.按首字母排序 在oracle9i中新增了按照拼音.部首.笔画排序功能.设置NLS_SORT值 SCHINESE_RADICA ...
- 获取手机通讯录放入PinnedSectionListView中,按名字首字母排序,并且实现拨打电话功能。
package com.lixu.tongxunlu; import java.util.ArrayList; import com.lixu.tongxunlu.PinnedSectionListV ...
- php按照中文首字母排序
1> 网络上很多php的工具类可以将汉字转为拼音: 2> 将拼音进行排序即可 另一种则是类似mysql转码方式: 1 foreach ($array as $key=>$value) ...
- HashMap加入数据后,会自动根据首字母排序
1.Map<String, ArrayList<XX>> entityHashMap = new HashMap<>(); 然后增加一些数据,会发现根据String ...
- mysql中文名字按首字母排序
在mysql数据库中可以使用GBK编码对中文进行排序,如名字按首字母排序 order by convert(substr(tu.username,1,1) using 'GBK') 其中substr方 ...
- mysql排序的中文首字母排序和自定义排序
select * FROM organ_new where city_code = 'SZ0755' and organ_type = 'H' and state = '1' ORDER BY FIE ...
- mysql、oracle 中按照拼音首字母排序
mysql中按照拼音首字母排序 convert(name using gbk) ASC 注:name 为字段名称 oracle中按照拼音首字母排序 nlssort(enterprise_name,'N ...
随机推荐
- C# 字符串中特定字符判断
/// <summary> /// 计算字符串中子串出现的次数 /// </summary> /// <param name="str">字符串 ...
- CentOS7.x使用yum安装Mysql5.6
先检查是否存在已安装的MySQL # yum list installed | grep mysql 若存在删除: yum remove 软件名称 CentOS 7的yum源中没有正常安装mysql时 ...
- Python学习---Python数据类型1206
1.1. 字符串格式化 字符格式化输出 占位符 %s s = string %d d = digit 整数 %f f = float 浮点数,约等于小数 #version: python3.2 ...
- 微信小程序及各种平台对接常用可逆加密算法aes256
不同程序之间经常会交换数据,我们经常采用的套路是: 假设要传输的信息是json,我们假设其为json_data,通过http传递信息为 json_data_encode=json_data&s ...
- 列表:List<T>与HashSet和只读集合
一.概述 List<T> 是ArrayList类的等效泛型类.属System.Collections.Generic命名空间. 二.声明及初始化 1.List<T> mList ...
- vue项目出现的错误汇总
报错一: expected "indent", got "!" 通过vue-cli创建的项目,不需要在webpack.base.conf.js中再手动配置关于c ...
- Codeforces Round #439 (Div. 2)【A、B、C、E】
Codeforces Round #439 (Div. 2) codeforces 869 A. The Artful Expedient 看不透( #include<cstdio> in ...
- lock与sychronized关键字
1. lock与sychronized关键字的区别 (1) Lock是一个接口,而synchronized是Java中的关键字,synchronized是内置的语言实现,Lock是代码层面的实现.(2 ...
- ctf经典好题复习
WEB200-2 这是swpu-ctf的一道题. <?php if(isset($_GET['user'])){ $login = @unserialize(base64_decode($_GE ...
- 2、Android-UI(RecyclerView)
2.6.滚动控件-RecylerView ListView虽然使用的效果很好但是也是有缺点的 不使用一些技巧来提升它的运行效率,性能就非常差 扩展性也不是很好 只能实现数据的纵向滚动效果 实现横向滚动 ...