开发中用到的方法,取出第一个汉字的首字母;

/**
* 取汉字的第一个字的首字母
* @param string $str
* @return string|null
*/
function getFirstChar($str) {
if (empty($str)) {
return '';
} $fir = $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;
if (!isset($s[0]) || !isset($s[1])) {
return '';
} $asc = ord($s[0]) * 256 + ord($s[1]) - 65536; if (is_numeric($str)) {
return $str;
} if (($asc >= -20319 && $asc <= -20284) || $fir == 'A') {
return 'A';
}
if (($asc >= -20283 && $asc <= -19776) || $fir == 'B') {
return 'B';
}
if (($asc >= -19775 && $asc <= -19219) || $fir == 'C') {
return 'C';
}
if (($asc >= -19218 && $asc <= -18711) || $fir == 'D') {
return 'D';
}
if (($asc >= -18710 && $asc <= -18527) || $fir == 'E') {
return 'E';
}
if (($asc >= -18526 && $asc <= -18240) || $fir == 'F') {
return 'F';
}
if (($asc >= -18239 && $asc <= -17923) || $fir == 'G') {
return 'G';
}
if (($asc >= -17922 && $asc <= -17418) || $fir == 'H') {
return 'H';
}
if (($asc >= -17417 && $asc <= -16475) || $fir == 'J') {
return 'J';
}
if (($asc >= -16474 && $asc <= -16213) || $fir == 'K') {
return 'K';
}
if (($asc >= -16212 && $asc <= -15641) || $fir == 'L') {
return 'L';
}
if (($asc >= -15640 && $asc <= -15166) || $fir == 'M') {
return 'M';
}
if (($asc >= -15165 && $asc <= -14923) || $fir == 'N') {
return 'N';
}
if (($asc >= -14922 && $asc <= -14915) || $fir == 'O') {
return 'O';
}
if (($asc >= -14914 && $asc <= -14631) || $fir == 'P') {
return 'P';
}
if (($asc >= -14630 && $asc <= -14150) || $fir == 'Q') {
return 'Q';
}
if (($asc >= -14149 && $asc <= -14091) || $fir == 'R') {
return 'R';
}
if (($asc >= -14090 && $asc <= -13319) || $fir == 'S') {
return 'S';
}
if (($asc >= -13318 && $asc <= -12839) || $fir == 'T') {
return 'T';
}
if (($asc >= -12838 && $asc <= -12557) || $fir == 'W') {
return 'W';
}
if (($asc >= -12556 && $asc <= -11848) || $fir == 'X') {
return 'X';
}
if (($asc >= -11847 && $asc <= -11056) || $fir == 'Y') {
return 'Y';
}
if (($asc >= -11055 && $asc <= -10247) || $fir == 'Z') {
return 'Z';
} return '';
}

php---------取汉字的第一个字的首字母的更多相关文章

  1. SQL汉字转拼音函数-支持首字母、全拼

    SQL汉字转拼音函数-支持首字母.全拼 FROM :http://my.oschina.net/ind/blog/191659 作者不详 --方法一sqlserver汉字转拼音首字母 --调用方法 s ...

  2. 使用PHP获取汉字的拼音(全部与首字母)

    <?php /** * 取汉字拼音 * edit by www.jbxue.com */ class GetPingYing { private $pylist = array( 'a'=> ...

  3. java汉字转拼音以及得到首字母通用方法

    package oa.common.utils;   import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.piny ...

  4. java根据汉字获取全拼和首字母

    import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCase ...

  5. vue集成汉字转拼音或提取首字母

    需求:             有时我们为了节省用户的维护量,需要根据中文生成出相应的拼音和缩写 解决:            此方法是利用汉字和Unicode编码对应找到相应字母 一.编写汉字和编码 ...

  6. 【Java】使用pinyin4j获取汉字的全拼或首字母

    汉字转拼音的工具类,常用于做汉字拼音的模糊查询. https://www.cnblogs.com/htyj/p/7891918.html

  7. php里获取第一个中文首字母并排序

    需求里结算首页需要按门店的首字母A-Z排序.我的数据结构原本是这样的: Array ( [0] => Array ( [sid] => 2885842 [recetcstoredpay] ...

  8. MySQL数据库获取多个汉字拼音的首字母函数

    需求简介:最近的一个项目,想实现如下图所示的显示效果.很明显,如果能够获取对应的汉字词组中每个汉字的拼音首字母就可以实现了,如果是固定的几组汉字,人为的拼一下就可以 了,不过项目中有多处功能需要这个效 ...

  9. Java 获取汉字串首字母并大写和获取汉字的全拼,英文字符不变

    在开发中我们难免会遇到需要提出汉字中的拼音的首字母.提出汉字的拼音,接着便介绍一个工具类 pinyin4j.jar,首先需要下载 jar 包. Pinyin4j是一个功能强悍的汉语拼音工具包,是sou ...

随机推荐

  1. SVN创建主干,分支、合并分支

    1.创建主干(trunk) 本文承接上文部分内容:http://www.cnblogs.com/dadonggg/p/8383696.html:部分不明,可以访问这篇文章. 当我们创建完代码仓库后,创 ...

  2. Augmented reality in natural scenes

    Augmented reality in natural scenes (Iryna Gordon and David Lowe)2006年关于AR的研究成果 项目主页 http://www.cs.u ...

  3. Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined)

    A. Bark to Unlock 题目链接:http://codeforces.com/contest/868/problem/A 题目意思:密码是两个字符组成的,现在你有n个由两个字符组成的字符串 ...

  4. nginx 上php不可写解决方法

    在php.ini中设置的session.save_path会被php-fpm.conf中覆盖 打开php-fpm.conf文件找到php_value['session.save_apth'] 这里的/ ...

  5. H5上传压缩图片

    看这个,比较全的 https://github.com/mhbseal/html5ImgCompress ,几乎所有痛点都解决了! PC上传图片 基本结构 form[enctype="mul ...

  6. Keras常用层

    Dense层:全连接层 Activatiion层:激活层,对一个层的输出施加激活函数 Dropout层:为输入数据施加Dropout.Dropout将在训练过程中每次更新参数时按一定概率(rate)随 ...

  7. top与with ties用法

    使用top中把与最后一条记录值相同的数据也放入列表中 一.SQL SERVER中使用WITH TIES的用途 with ties一般是和Top , order by相结合使用的,会查询出最后一条数据额 ...

  8. jquery序列化表单以及回调函数的使用

    在开发项目中.将前台的值传给后台,有时的JSP表单中的值有一两个,也有所有的值,假设这时一个个传,必然不是非常好的办法,所以使用jQuery提供的表单序列化方法,能够非常好的解决问题.同一时候能够封装 ...

  9. 初识MVCSharp

    MVCSharp其实是MVP模式 主要内容 ITask IView IController

  10. maven解决“Could not calculate build plan”问题

    错误提示如下:(eclipse+maven) Could not calculate build plan: Failure to transfer org.apache.maven.plugins: ...