在我们实际开发工作中,有时候需要获取输入汉字的首字母,然后存到库中,方便用户搜索相关信息,下面给出php代码,留做备用:

//新添加获取汉子首字,首字字母
function pinyin($zh){
if(empty($zh)){
return '';
}
$s1 = iconv("UTF-8","gb2312", $zh);
$s2 = iconv("gb2312","UTF-8", $s1);//将字符串的编码从GB2312转到UTF-8
if($s2 == $zh){$zh = $s1;}
$s1 = substr($zh,0,1);
$p = ord($s1);
if($p > 160){
$s2 = substr($zh,0,2);
return strtoupper(self::getfirstchar($s2));
}else{
return strtoupper($s1);
}
} function getfirstchar($s0){
$fchar = ord($s0{0});
if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($s0{0});
$s1 = self::get_encoding($s0,'GB2312');
$s2 = self::get_encoding($s1,'UTF-8');
if($s2 == $s0){$s = $s1;}else{$s = $s0;}
$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 <= -17923) return "G";
if($asc >= -17922 and $asc <= -17418) return "H";
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";
return null;
}

php获取汉字首字母实例的更多相关文章

  1. JS实现获取汉字首字母拼音、全拼音及混拼音的方法

    本文实例讲述了JS实现获取汉字首字母拼音.全拼音及混拼音的方法.分享给大家供大家参考,具体如下: 这里需要用到一个js获取汉字拼音的插件,可点击此处本站下载. 运行效果如下: 完整示例代码: ? 1 ...

  2. JS获取汉字首字母

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

  3. ASP.NET获取汉字首字母

    /// <summary> /// 获取汉字首字母(可包含多个汉字) /// </summary> /// <param name="strText" ...

  4. php获取汉字首字母

    php获取汉字首字母,可以用于按字母对数据进行检索排序等. 分享下网上找的代码.亲测有效. function getFirstCharter($str){ if(empty($str)){return ...

  5. .NET获取汉字首字母

    /// <summary> /// 获取汉字首字母(可包含多个汉字) /// </summary> /// <param name="strText" ...

  6. python获取汉字首字母

    获取汉字首字母 关注公众号"轻松学编程"了解更多. 应用场景之一:可用于获取名字首字母,在数据库中查询记录时,可以用它来排序输出. from pytz import unicode ...

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

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

  8. PHP获取汉字首字母并分组排序

    <?php /** * 错误状态码定义 * User: xiucai * Date: 2018/3/11 * Time: 12:23 */ namespace extend; class Wor ...

  9. c#获取汉字首字母拼音

    /* * 由SharpDevelop创建. * 用户: lenovo * 日期: 2013/10/22 * 时间: 20:15 * * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件 */ ...

随机推荐

  1. Oracle中使用游标获取指定数据表的所有字段名对应的字符串

    操作步骤:打开PLSQL Developer后,直接执行下面的语句就可以出来 --Oracle中使用游标获取指定数据表的所有字段名对应的字符串 declare mytablename VARCHAR( ...

  2. Chapter 2 User Authentication, Authorization, and Security(6):服务器权限授予粒度

    原文出处:http://blog.csdn.net/dba_huangzj/article/details/38867489,专题目录:http://blog.csdn.net/dba_huangzj ...

  3. PA模块常用表

    SELECT * FROM pa_projects_all; --项目 SELECT * FROM pa_project_types; --项目类型 SELECT * FROM pa_project_ ...

  4. 软考之路--J2SE,从HelloWorld到传世代码

    "Hello World"程序指的是只在计算机屏幕上输出"Hello, World!"(意为"世界,你好!")这行字符串的计算机程序.hel ...

  5. 安卓TV开发(五) 移动智能终端UI之实现主流TV焦点可控UI

      载请标明出处:http://blog.csdn.net/sk719887916,作者:skay    由于其他网站收录,导致你无法查看本系列原创文章请点击此处 安卓TV开发(四)实现主流智能T ...

  6. C语言函数strstr()分析及实现

    原型:char *strstr(const char *str1, const char *str2); #include<string.h> 找出str2字符串在str1字符串中第一次出 ...

  7. 用U盘安装Ubuntu15.04

    用UltraISO刻录Ubuntu15.04到U盘安装,出现:Failed to load idlinux.c32错误,解决办法如下: source url: http://www.ubuntukyl ...

  8. Leetcode_154_Find Minimum in Rotated Sorted Array

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43416613 Suppose a sorted array ...

  9. 【生活随笔】Introspection of my life in 2014

       2014年已过去两星期,有写年度总结的必要了.今天特意看了看去年1月5日写的2013年度总结,看看都有些什么变化.我发现每年作一次总结是很有必要的,无赖恰逢考试周,连元旦都不能好好过,更不用说写 ...

  10. C语言字符串的常见特殊操作(除了string.c实现的那些接口)

    字符串操作中,必须掌握的一些之前已经在文章有写过了,比如说字符串查找,字符串粘帖,字符串拷贝等等,这些在标准C库的string.c中已经有实现,故包含#include <string.h> ...