PHP获取中文首字母的函数
PHP获取中文首字母的函数
<?php
header("Content-type: text/html; charset=utf-8"); function getFirstCharter($str)
{
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 null; }
$array=array(
0=>array('msg'=>'安培','val'=>123),
1=>array('msg'=>'暴力','val'=>233)
); foreach($array as $k=>&$v) {
$v['letter']=strtolower(getFirstCharter($v['msg']));
}
var_dump($array);
PHP获取中文首字母的函数的更多相关文章
- php获取汉字首字母的函数
本文介绍用php实现汉字转化为首字母的方法,主要功能是:功能明确,易于修改维护和扩展: 英文的字串:不变返回(包括数字):中文字符串:返回拼音首字符: 中英混合串: 返回拼音首字符和英文. 网上的方法 ...
- php中获取中文首字母程序代码
年会抽奖,要求一等奖的中奖概率是0.12%,二等奖中奖概率是3%,三等奖中奖概率是12%,其他中奖概率是都是谢谢惠顾. <?php /** * 抽奖 * @param int $total */ ...
- Java获取中文拼音、中文首字母缩写和中文首字母
获取中文拼音(如:广东省 -->guangdongsheng) /** * 得到中文全拼 * @param src 需要转化的中文字符串 * @return */ public static S ...
- JS获取汉字首字母
//获取 汉字首字母 function makePy(str) { if (typeof (str) != "string") throw new Error(-1, " ...
- ms sql 获取字符串首字母
很久没有编写新文章,现在发布一篇自定义函数,针对于ms sql数据库中需要获取字符串首字母,对于需要的朋友希望对你有用,如果你有更好的方法也可以给我留言.函数如下: --获取字符串首字母 CREATE ...
- php按照中文首字母排序
1> 网络上很多php的工具类可以将汉字转为拼音: 2> 将拼音进行排序即可 另一种则是类似mysql转码方式: 1 foreach ($array as $key=>$value) ...
- js中文首字母数组排序
js中文首字母数组排序 数组的排序js算法: var Pinyin = (function() { var Pinyin = function(ops) { this.initialize(ops); ...
- Java中实现对集合中对象按中文首字母排序
有一个person对象如下: public class Person { private String id;private String nam; } 一个list集合如下: List<Emp ...
- C#获取字符首字母
///<summary> /// 获取字符首字母 /// </summary> public static string GetPyChar(string c) { if (s ...
随机推荐
- 服务器病了吗? Linux 服务器的那些性能参数指标
一个基于 Linux 操作系统的服务器运行的同时,也会表征出各种各样参数信息.通常来说运维人员.系统管理员会对这些数据会极为敏感,但是这些参数对于开发者来说也十分重要,尤其当你的程序非正常工作的时候, ...
- Docker安装 和简单使用
1.安装依赖 yum install -y yum-utils device-mapper-persistent-data lvm2 2.这一步设置即将安装的是稳定版仓库 yum-config-man ...
- 《JavaScript设计模式与开发》笔记 3.call和apply
1.改变this指向 2.Function.prototype.bind 3.借用其他对象方法 1.借用实现继承 2.实现恶心的 Array.prototype.push.call Array.pro ...
- 【亲测】<g++/gcc>CentOS下g++: command not found问题的解决(c++环境安装)
CentOS下g++: command not found问题的解决 2017年02月27日 18:09:06 阅读数:5174 标签: centosgcc 更多 个人分类: 问题分析 版权声明: ...
- RTB业务知识之1-原生广告
一.背景 Native Advertising (Native Ads), 又称为原生广告, 是2013全球媒体界爆红的关键词,从2012年年底,就有人开始提了这个名词,接着到处都可以看到这个名词,再 ...
- Centos7 安装sz,rz命令
yum install lrzsz 我记得以前某个我敬佩的人说过压缩分很多种,有空,补充这篇笔记.加油~
- Maven打包将所有的依赖都打入
附上pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...
- Netty简单的HTTP服务器
本文转载自:https://www.cnblogs.com/luangeng/p/7875710.html HTTP协议:略 基于Netty的HTTP协议栈可以方便的进行异步非阻塞的HTTP服务器的开 ...
- 廖雪峰Java2面向对象编程-3继承和多态-1继承
1.继承 继承是一种代码复用的方式. Student与Person有相同部分的代码. Student可以从Person继承,这样Student获得了Person的所有功能,只需要编写新增的功能即可.通 ...
- Linux下设置固定IP的方法
本文转自http://blog.163.com/liulina_517@126/blog/static/3766198320118231431594/ linux系统安装完,以后通过命令模式配置网卡I ...