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获取中文首字母的函数的更多相关文章

  1. php获取汉字首字母的函数

    本文介绍用php实现汉字转化为首字母的方法,主要功能是:功能明确,易于修改维护和扩展: 英文的字串:不变返回(包括数字):中文字符串:返回拼音首字符: 中英混合串: 返回拼音首字符和英文. 网上的方法 ...

  2. php中获取中文首字母程序代码

    年会抽奖,要求一等奖的中奖概率是0.12%,二等奖中奖概率是3%,三等奖中奖概率是12%,其他中奖概率是都是谢谢惠顾. <?php /** * 抽奖 * @param int $total */ ...

  3. Java获取中文拼音、中文首字母缩写和中文首字母

    获取中文拼音(如:广东省 -->guangdongsheng) /** * 得到中文全拼 * @param src 需要转化的中文字符串 * @return */ public static S ...

  4. JS获取汉字首字母

    //获取 汉字首字母 function makePy(str) { if (typeof (str) != "string") throw new Error(-1, " ...

  5. ms sql 获取字符串首字母

    很久没有编写新文章,现在发布一篇自定义函数,针对于ms sql数据库中需要获取字符串首字母,对于需要的朋友希望对你有用,如果你有更好的方法也可以给我留言.函数如下: --获取字符串首字母 CREATE ...

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

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

  7. js中文首字母数组排序

    js中文首字母数组排序 数组的排序js算法: var Pinyin = (function() { var Pinyin = function(ops) { this.initialize(ops); ...

  8. Java中实现对集合中对象按中文首字母排序

    有一个person对象如下: public class Person { private String id;private String nam; } 一个list集合如下: List<Emp ...

  9. C#获取字符首字母

    ///<summary> /// 获取字符首字母 /// </summary> public static string GetPyChar(string c) { if (s ...

随机推荐

  1. paramiko 实现ssh登录和sftp登录

    简单ssh登录 import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddP ...

  2. XBOX360

    [汇总+分享]XBOX360多人游戏汇总贴https://tieba.baidu.com/p/3550398060?pn=13&red_tag=3423139816&traceid= ...

  3. 学习java字符串编码总结

    http://blog.csdn.net/wikijava/article/details/5571953 系统不同部分的编码方式转换

  4. php大量数据 10M数据从查询到下载 【内存溢出,查询过慢】解决方案

    功能描述:做数据导出 功能分析:1.采用csv的格式,因为csv的格式比excel小 2. 3W条数据,100个字段需要全部导出 开始 直接查询 //此处使用的laravel框架,具体含义一看就懂 t ...

  5. 关于JAVA架构师

    在我们行业内,我们大致把程序员分为四级 1.初级Java程序员的重心在编写代码.运用框架: 2.中级Java程序员重心在编写代码和框架: 3.高级Java程序员技术攻关.性能调优: 4.架构师 解决业 ...

  6. 搜索引擎(lucene及周边) 涉及的一些算法总结

    一)分词 1)正向/逆向最大匹配算法 典型:IKAnalyzer采用的是正向迭代最细粒度切分算法 IKAnalyzer源码简单分析: http://www.cnblogs.com/huangfox/p ...

  7. Hadoop概念学习系列之Hadoop、Spark学习路线(很值得推荐)(十八)

    不多说,直接上干货! 说在前面的话 此笔,对于仅对于Hadoop和Spark初中学者.高手请忽略! 1 Java基础: 视频方面:          推荐<毕向东JAVA基础视频教程>.学 ...

  8. 2.C++语言特性

    一.普遍编程语言的特征 任何常用的编程语言都具备一组公共的语法特征,不同的语言仅在特征的细节上有所区别.所以,要想掌握一门语言,需要理解其语法特征的实现细节是第一步.  最基本的特征包括:       ...

  9. java高并发编程(四)高并发的一些容器

    摘抄自马士兵java并发视频课程: 一.需求背景: 有N张火车票,每张票都有一个编号,同时有10个窗口对外售票, 请写一个模拟程序. 分析下面的程序可能会产生哪些问题?重复销售?超量销售? /** * ...

  10. JSON: 介绍、应用

    ylbtech-JSON:  介绍.应用 JSONP(JSON with Padding)是 JSON 的一种“使用模式”,可以让网页从别的域名(网站)那获取资料,即跨域读取数据. 为什么我们从不同的 ...