PHP获取汉字首字母并分组排序
<?php
/**
* 错误状态码定义
* User: xiucai
* Date: 2018/3/11
* Time: 12:23
*/ namespace extend; class Wordsgroup
{ /**
* 二维数组根据首字母分组排序
* @param array $data 二维数组
* @param string $targetKey 首字母的键名
* @return array 根据首字母关联的二维数组
*/
public function groupByInitials(array $data, $targetKey = 'name')
{
$word=[];
foreach ($data as $k=> $v) {
$key=$this->getInitials($v[$targetKey]);
if(isset($word[$key])){
array_push($word[$key],$data[$k]);
}else{
$word[$key][]=$data[$k];
}
} ksort($word);
return $word;
} /**
* 获取首字母
* @param string $str 汉字字符串
* @return string 首字母
*/
public function getInitials($str)
{
error_reporting('E_ALL');
try {
if (empty($str)) {return '';}
$fchar = ord($str{0});
if ($fchar >= ord('A') && $fchar <= ord('z')) {
return strtoupper($str{0});
} $s1 = iconv('UTF-8', 'gb2312', $str);
$s2 = iconv('gb2312', 'UTF-8', $s1);
$s = $s2 == $str ? $s1 : $str;
$asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
if ($asc >= -20319 && $asc <= -20284) {
return 'A';
} if ($asc >= -20283 && $asc <= -19776) {
return 'B';
} if ($asc >= -19775 && $asc <= -19219) {
return 'C';
} if ($asc >= -19218 && $asc <= -18711) {
return 'D';
} if ($asc >= -18710 && $asc <= -18527) {
return 'E';
} if ($asc >= -18526 && $asc <= -18240) {
return 'F';
} if ($asc >= -18239 && $asc <= -17923) {
return 'G';
} if ($asc >= -17922 && $asc <= -17418) {
return 'H';
} if ($asc >= -17417 && $asc <= -16475) {
return 'J';
} if ($asc >= -16474 && $asc <= -16213) {
return 'K';
} if ($asc >= -16212 && $asc <= -15641) {
return 'L';
} if ($asc >= -15640 && $asc <= -15166) {
return 'M';
} if ($asc >= -15165 && $asc <= -14923) {
return 'N';
} if ($asc >= -14922 && $asc <= -14915) {
return 'O';
} if ($asc >= -14914 && $asc <= -14631) {
return 'P';
} if ($asc >= -14630 && $asc <= -14150) {
return 'Q';
} if ($asc >= -14149 && $asc <= -14091) {
return 'R';
} if ($asc >= -14090 && $asc <= -13319) {
return 'S';
} if ($asc >= -13318 && $asc <= -12839) {
return 'T';
} if ($asc >= -12838 && $asc <= -12557) {
return 'W';
} if ($asc >= -12556 && $asc <= -11848) {
return 'X';
} if ($asc >= -11847 && $asc <= -11056) {
return 'Y';
} if ($asc >= -11055 && $asc <= -10247) {
return 'Z';
} return '其他';
}catch (Exception $e) {
return '其他';
}
} }
PHP获取汉字首字母并分组排序的更多相关文章
- 获取汉字首字母并分组排列 PHP
1.代码class Character{ /** * 数组根据首首字母排序 */ /** * 二维数组根据首字母分组排序 * @param array $data 二维数组 * @param stri ...
- php获取汉字首字母
php获取汉字首字母,可以用于按字母对数据进行检索排序等. 分享下网上找的代码.亲测有效. function getFirstCharter($str){ if(empty($str)){return ...
- python获取汉字首字母
获取汉字首字母 关注公众号"轻松学编程"了解更多. 应用场景之一:可用于获取名字首字母,在数据库中查询记录时,可以用它来排序输出. from pytz import unicode ...
- JS获取汉字首字母
//获取 汉字首字母 function makePy(str) { if (typeof (str) != "string") throw new Error(-1, " ...
- ASP.NET获取汉字首字母
/// <summary> /// 获取汉字首字母(可包含多个汉字) /// </summary> /// <param name="strText" ...
- JS实现获取汉字首字母拼音、全拼音及混拼音的方法
本文实例讲述了JS实现获取汉字首字母拼音.全拼音及混拼音的方法.分享给大家供大家参考,具体如下: 这里需要用到一个js获取汉字拼音的插件,可点击此处本站下载. 运行效果如下: 完整示例代码: ? 1 ...
- .NET获取汉字首字母
/// <summary> /// 获取汉字首字母(可包含多个汉字) /// </summary> /// <param name="strText" ...
- php获取汉字首字母的函数
本文介绍用php实现汉字转化为首字母的方法,主要功能是:功能明确,易于修改维护和扩展: 英文的字串:不变返回(包括数字):中文字符串:返回拼音首字符: 中英混合串: 返回拼音首字符和英文. 网上的方法 ...
- php获取汉字首字母实例
在我们实际开发工作中,有时候需要获取输入汉字的首字母,然后存到库中,方便用户搜索相关信息,下面给出php代码,留做备用: //新添加获取汉子首字,首字字母 function pinyin($zh){ ...
随机推荐
- C#中字符串转日期类型
1,yyyyMMdd DateTime date = DateTime.ParseExact(", "yyyyMMdd", System.Globalization.Cu ...
- MDK 编译错误和警告 使用时遇到的小问题
main.c(32): warning: #1-D: last line of file ends without a newline 这个是由于在main函数的“}”后,没有加回车. 只要在mai ...
- php对某个页面设置基础认证登录设置
记录下. 对某个页面设置认证 代码最开始添加: <?php $user = 'test'; $password = '111111'; // 通过HTTP认证进行验证 if (!isset($_ ...
- 利用反射将IDataReader读取到实体类中效率低下的解决办法
最开始使用反射一个类型的各个属性,对气进行赋值的代码如下: public static List<T> ToList<T>(IDataReader reader) { //实例 ...
- 微信小程序地图控件篇 ---自定义图标被地图覆盖的问题
今天在做微信小程序的时候遇到这个这样的问题 需要在地图上加个一个自定义的图标控件 类似这样的 刚开始的时候怎图片一直会被地图组件覆盖 ,要怎么解决这个问题 我去翻了下小程序的文档 有个cover ...
- python_类与对象学习笔记
class Phone: #手机属性===>类属性 # color='black' # price=4500 # brand='oppo' # size='5.5' #参数化-魔法方法--初始化 ...
- python全栈开发中级班全程笔记(第二模块、第四章)(常用模块导入)
python全栈开发笔记第二模块 第四章 :常用模块(第二部分) 一.os 模块的 详解 1.os.getcwd() :得到当前工作目录,即当前python解释器所在目录路径 impor ...
- 二、docker的安装和基本命令
一.docker的安装 1.安装依赖 # yum install -y yum-utils device-mapper-persistent-data 2.设置yum源 # yum-config-ma ...
- .Net Core在Centos7上初体验
本文主要内容是简单介绍如何在centos7上开发.Net Core项目,在此之前我们首先了解下.Net Core的基本特性. 1 .Net Core和.Net FrameWork的异同 1.1 .Ne ...
- 树莓派安装 MySQL 时出现错误的解决方法
今天被要求解决一个树莓派无法正常安装 MySQL 的问题.以下是解决过程记录. 我在 Mac 上利用 SSH 连接到树莓派,执行 sudo apt-get install mysql-server m ...