获取多个汉字首字母(php)

 1 <?php
 2 function getfirstchar($s0){
 3     $fchar = ord($s0{0});
 4     if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($s0{0});
 5     $s1 = iconv("UTF-8","gb2312", $s0);
 6     $s2 = iconv("gb2312","UTF-8", $s1);
 7     if($s2 == $s0){$s = $s1;}else{$s = $s0;}
 8     $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
 9     if($asc >= -20319 and $asc <= -20284) return "A";
10     if($asc >= -20283 and $asc <= -19776) return "B";
11     if($asc >= -19775 and $asc <= -19219) return "C";
12     if($asc >= -19218 and $asc <= -18711) return "D";
13     if($asc >= -18710 and $asc <= -18527) return "E";
14     if($asc >= -18526 and $asc <= -18240) return "F";
15     if($asc >= -18239 and $asc <= -17923) return "G";
16     if($asc >= -17922 and $asc <= -17418) return "I";
17     if($asc >= -17417 and $asc <= -16475) return "J";
18     if($asc >= -16474 and $asc <= -16213) return "K";
19     if($asc >= -16212 and $asc <= -15641) return "L";
20     if($asc >= -15640 and $asc <= -15166) return "M";
21     if($asc >= -15165 and $asc <= -14923) return "N";
22     if($asc >= -14922 and $asc <= -14915) return "O";
23     if($asc >= -14914 and $asc <= -14631) return "P";
24     if($asc >= -14630 and $asc <= -14150) return "Q";
25     if($asc >= -14149 and $asc <= -14091) return "R";
26     if($asc >= -14090 and $asc <= -13319) return "S";
27     if($asc >= -13318 and $asc <= -12839) return "T";
28     if($asc >= -12838 and $asc <= -12557) return "W";
29     if($asc >= -12556 and $asc <= -11848) return "X";
30     if($asc >= -11847 and $asc <= -11056) return "Y";
31     if($asc >= -11055 and $asc <= -10247) return "Z";
32     return null;
33 }
34
35
36 function pinyin1($zh){
37     $ret = "";
38     $s1 = iconv("UTF-8","gb2312", $zh);
39     $s2 = iconv("gb2312","UTF-8", $s1);
40     if($s2 == $zh){$zh = $s1;}
41     for($i = 0; $i < strlen($zh); $i++){
42         $s1 = substr($zh,$i,1);
43         $p = ord($s1);
44         if($p > 160){
45             $s2 = substr($zh,$i++,2);
46             $ret .= getfirstchar($s2);
47         }else{
48             $ret .= $s1;
49         }
50     }
51     return $ret;
52 }
53 echo "北京信息学校<br/>";
54 echo pinyin1(‘北京信息学校‘);

感谢网友分享

时间: 2024-12-10 23:04:07

获取多个汉字首字母(php)的相关文章

php获取汉字首字母

php获取汉字首字母,可以用于按字母对数据进行检索排序等. 分享下网上找的代码.亲测有效. 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('gb

获取汉字首字母

//获取汉字拼音的第一个字母 static public string GetChineseSpell(string strText) { int len = strText.Length; string myStr = ""; for (int i = 0; i < len; i++) { myStr += getSpell(strText.Substring(i, 1)); } return myStr; } static public string[] GetChinese

ASP.NET获取汉字首字母

/// <summary> /// 获取汉字首字母(可包含多个汉字) /// </summary> /// <param name="strText"></param> /// <returns></returns> public string GetChineseSpell(string strText) { int len = strText.Length; string myStr = ""

4月02日 提取汉字首字母,并大写的类

  public class GetStringSpell    {        /// <summary>          /// 提取汉字首字母          /// </summary>          /// <param name="strText">需要转换的字</param>          /// <returns>转换结果</returns>          public strin

mysql和SqlServer 中取得汉字字段的各汉字首字母

mysql 中取得汉字字段的各汉字首字母 这个转载于http://blog.csdn.net/lky5387/article/details/11973721 DELIMITER ;;CREATE  FUNCTION `getPY`(in_string VARCHAR(65534)) RETURNS mediumtext CHARSET utf8BEGINDECLARE tmp_str VARCHAR(65534) charset gbk DEFAULT '' ; #截取字符串,每次做截取后的字

react-native按照汉字首字母排序

问题讨论详情:https://github.com/facebook/react-native/issues/12597 问题描述: JS中有一个String的内置函数:String.prototype.localeCompare(),返回一个数字来指示一个参考字符串是否在排序顺序前面或之后或与给定字符串相同. 1 a.nickName.localeCompare(b.nickName, 'zh-Hans-CN', {sensitivity: 'accent'}) 在react-native环境

在数据库中将中文转换为拼音或者汉字首字母 转

sql数据库自定义一个函数把下面代码写进去 功能是得到汉字拼音首字母 如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 create function fun_getPY(@str nvarchar(4000)) returns nvarchar(4000) as begin declare @word n

php获取中文字符拼音首字母

//php获取中文字符拼音首字母 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',$s

php获取中文字符拼音首字母 阿星小栈

//php获取中文字符拼音首字母 public function getFirstWord($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=$s