php中怎样获取汉字拼音的首字母

php中怎样获取汉字拼音的首字母,第1张

//汉字拼音(生僻字不支持)

 $pinyin = $this->utf8_to("我是中文");

    //汉字转拼音(生僻字不支持)

   function(){

      echo $this->utf8_to("我是个好人");

   }

   

   /

     以下是中文转拼音

     @param $s

     @param bool $isfirst

     @return string

    /

   public static function utf8_to($s, $isfirst = false) {

       return self::to(self::utf8_to_gb2312($s), $isfirst);

   }

   public static function utf8_to_gb2312($s) {

       return iconv('UTF-8', 'GB2312//IGNORE', $s);

   }

// 字符串必须为GB2312编码

   public static function to($s, $isfirst = false) {

       $res = '';

       $len = strlen($s);

       $pinyin_arr = self::get_pinyin_array();

       for($i=0; $i<$len; $i++) {

           $ascii = ord($s{$i});

           if($ascii > 0x80) {

               $ascii2 = ord($s{++$i});

               $ascii = $ascii  256 + $ascii2 - 65536;

           }

           if($ascii < 255 && $ascii > 0) {

               if(($ascii >= 48 && $ascii <= 57) || ($ascii >= 97 && $ascii <= 122)) {

                   $res = $s{$i}; // 0-9 a-z

               }elseif($ascii >= 65 && $ascii <= 90) {

                   $res = strtolower($s{$i}); // A-Z

               }else{

                   $res = '_';

               }

           }elseif($ascii < -20319 || $ascii > -10247) {

               $res = '_';

           }else{

               foreach($pinyin_arr as $py=>$asc) {

                   if($asc <= $ascii) {

                       $res = $isfirst  $py{0} : $py;

                       break;

                   }

               }

           }

       }

       return $res;

   }

   public static function to_first($s) {

       $ascii = ord($s{0});

       if($ascii > 0xE0) {

           $s = self::utf8_to_gb2312($s{0}$s{1}$s{2});

       }elseif($ascii < 0x80) {

           if($ascii >= 65 && $ascii <= 90) {

               return strtolower($s{0});

           }elseif($ascii >= 97 && $ascii <= 122) {

               return $s{0};

           }else{

               return false;

           }

       }

       if(strlen($s) < 2) {

           return false;

       }

       $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 false;

   }

   public static function get_pinyin_array() {

       static $py_arr;

       if(isset($py_arr)) return $py_arr;

       $k = 'a|ai|an|ang|ao|ba|bai|ban|bang|bao|bei|ben|beng|bi|bian|biao|bie|bin|bing|bo|bu|ca|cai|can|cang|cao|ce|ceng|cha|chai|chan|chang|chao|che|chen|cheng|chi|chong|chou|chu|chuai|chuan|chuang|chui|chun|chuo|ci|cong|cou|cu|cuan|cui|cun|cuo|da|dai|dan|dang|dao|de|deng|di|dian|diao|die|ding|diu|dong|dou|du|duan|dui|dun|duo|e|en|er|fa|fan|fang|fei|fen|feng|fo|fou|fu|ga|gai|gan|gang|gao|ge|gei|gen|geng|gong|gou|gu|gua|guai|guan|guang|gui|gun|guo|ha|hai|han|hang|hao|he|hei|hen|heng|hong|hou|hu|hua|huai|huan|huang|hui|hun|huo|ji|jia|jian|jiang|jiao|jie|jin|jing|jiong|jiu|ju|juan|jue|jun|ka|kai|kan|kang|kao|ke|ken|keng|kong|kou|ku|kua|kuai|kuan|kuang|kui|kun|kuo|la|lai|lan|lang|lao|le|lei|leng|li|lia|lian|liang|liao|lie|lin|ling|liu|long|lou|lu|lv|luan|lue|lun|luo|ma|mai|man|mang|mao|me|mei|men|meng|mi|mian|miao|mie|min|ming|miu|mo|mou|mu|na|nai|nan|nang|nao|ne|nei|nen|neng|ni|nian|niang|niao|nie|nin|ning|niu|nong|nu|nv|nuan|nue|nuo|o|ou|pa|pai|pan|pang|pao|pei|pen|peng|pi|pian|piao|pie|pin|ping|po|pu|qi|qia|qian|qiang|qiao|qie|qin|qing|qiong|qiu|qu|quan|que|qun|ran|rang|rao|re|ren|reng|ri|rong|rou|ru|ruan|rui|run|ruo|sa|sai|san|sang|sao|se|sen|seng|sha|shai|shan|shang|shao|she|shen|sheng|shi|shou|shu|shua|shuai|shuan|shuang|shui|shun|shuo|si|song|sou|su|suan|sui|sun|suo|ta|tai|tan|tang|tao|te|teng|ti|tian|tiao|tie|ting|tong|tou|tu|tuan|tui|tun|tuo|wa|wai|wan|wang|wei|wen|weng|wo|wu|xi|xia|xian|xiang|xiao|xie|xin|xing|xiong|xiu|xu|xuan|xue|xun|ya|yan|yang|yao|ye|yi|yin|ying|yo|yong|you|yu|yuan|yue|yun|za|zai|zan|zang|zao|ze|zei|zen|zeng|zha|zhai|zhan|zhang|zhao|zhe|zhen|zheng|zhi|zhong|zhou|zhu|zhua|zhuai|zhuan|zhuang|zhui|zhun|zhuo|zi|zong|zou|zu|zuan|zui|zun|zuo';

       $v = '-20319|-20317|-20304|-20295|-20292|-20283|-20265|-20257|-20242|-20230|-20051|-20036|-20032|-20026|-20002|-19990|-19986|-19982|-19976|-19805|-19784|-19775|-19774|-19763|-19756|-19751|-19746|-19741|-19739|-19728|-19725|-19715|-19540|-19531|-19525|-19515|-19500|-19484|-19479|-19467|-19289|-19288|-19281|-19275|-19270|-19263|-19261|-19249|-19243|-19242|-19238|-19235|-19227|-19224|-19218|-19212|-19038|-19023|-19018|-19006|-19003|-18996|-18977|-18961|-18952|-18783|-18774|-18773|-18763|-18756|-18741|-18735|-18731|-18722|-18710|-18697|-18696|-18526|-18518|-18501|-18490|-18478|-18463|-18448|-18447|-18446|-18239|-18237|-18231|-18220|-18211|-18201|-18184|-18183|-18181|-18012|-17997|-17988|-17970|-17964|-17961|-17950|-17947|-17931|-17928|-17922|-17759|-17752|-17733|-17730|-17721|-17703|-17701|-17697|-17692|-17683|-17676|-17496|-17487|-17482|-17468|-17454|-17433|-17427|-17417|-17202|-17185|-16983|-16970|-16942|-16915|-16733|-16708|-16706|-16689|-16664|-16657|-16647|-16474|-16470|-16465|-16459|-16452|-16448|-16433|-16429|-16427|-16423|-16419|-16412|-16407|-16403|-16401|-16393|-16220|-16216|-16212|-16205|-16202|-16187|-16180|-16171|-16169|-16158|-16155|-15959|-15958|-15944|-15933|-15920|-15915|-15903|-15889|-15878|-15707|-15701|-15681|-15667|-15661|-15659|-15652|-15640|-15631|-15625|-15454|-15448|-15436|-15435|-15419|-15416|-15408|-15394|-15385|-15377|-15375|-15369|-15363|-15362|-15183|-15180|-15165|-15158|-15153|-15150|-15149|-15144|-15143|-15141|-15140|-15139|-15128|-15121|-15119|-15117|-15110|-15109|-14941|-14937|-14933|-14930|-14929|-14928|-14926|-14922|-14921|-14914|-14908|-14902|-14894|-14889|-14882|-14873|-14871|-14857|-14678|-14674|-14670|-14668|-14663|-14654|-14645|-14630|-14594|-14429|-14407|-14399|-14384|-14379|-14368|-14355|-14353|-14345|-14170|-14159|-14151|-14149|-14145|-14140|-14137|-14135|-14125|-14123|-14122|-14112|-14109|-14099|-14097|-14094|-14092|-14090|-14087|-14083|-13917|-13914|-13910|-13907|-13906|-13905|-13896|-13894|-13878|-13870|-13859|-13847|-13831|-13658|-13611|-13601|-13406|-13404|-13400|-13398|-13395|-13391|-13387|-13383|-13367|-13359|-13356|-13343|-13340|-13329|-13326|-13318|-13147|-13138|-13120|-13107|-13096|-13095|-13091|-13076|-13068|-13063|-13060|-12888|-12875|-12871|-12860|-12858|-12852|-12849|-12838|-12831|-12829|-12812|-12802|-12607|-12597|-12594|-12585|-12556|-12359|-12346|-12320|-12300|-12120|-12099|-12089|-12074|-12067|-12058|-12039|-11867|-11861|-11847|-11831|-11798|-11781|-11604|-11589|-11536|-11358|-11340|-11339|-11324|-11303|-11097|-11077|-11067|-11055|-11052|-11045|-11041|-11038|-11024|-11020|-11019|-11018|-11014|-10838|-10832|-10815|-10800|-10790|-10780|-10764|-10587|-10544|-10533|-10519|-10331|-10329|-10328|-10322|-10315|-10309|-10307|-10296|-10281|-10274|-10270|-10262|-10260|-10256|-10254';

       $key = explode('|', $k);

       $val = explode('|', $v);

       $py_arr = array_combine($key, $val);

       arsort($py_arr);

       return $py_arr;

   }

其实就是一个记录的问题。

比如你可以在建表时,就创建一个在线表,其中可以包含在线用户ID号、用户名等等内容。

通常情况下该表是空的,当有用户登录时,就将其ID号写入该表,手动退出时,清除表中记录。

这样当要显示在线用户列表时,只需要遍历这个表就行。

还有一种方法是在创建用户信息表时,预留一个字段为:on_line,其类项为布尔型,默认值是false。当对应用户登录时,将该值update为true,当用户手动退出时将该值重置为false。

这样,当要显示在线用户时,直接遍历用户表时使用条件where on_line=true即可。

可以的,自己做的PHP函数可以实现。

用法

1先导入这个文件,include pinyinphp

2,使用函数转换,在echo后写入待转换中文,即可输出为汉字的拼音形式。全部为字母。

3,echo('汉字转换拼音')---输出为hanzizhuanhuanpinyin

PHP汉字转换函数文件pinyinphp如下。如果想编辑,可使用editplus打开。

<php function Pinyin($_String, $_Code='gb2312') { $_DataKey = "a|ai|an|ang|ao|ba|bai|ban|bang|bao|bei|ben|beng|bi|bian|biao|bie|bin|bing|bo|bu|ca|cai|can|cang|cao|ce|ceng|cha" "|chai|chan|chang|chao|che|chen|cheng|chi|chong|chou|chu|chuai|chuan|chuang|chui|chun|chuo|ci|cong|cou|cu|" "cuan|cui|cun|cuo|da|dai|dan|dang|dao|de|deng|di|dian|diao|die|ding|diu|dong|dou|du|duan|dui|dun|duo|e|en|er" "|fa|fan|fang|fei|fen|feng|fo|fou|fu|ga|gai|gan|gang|gao|ge|gei|gen|geng|gong|gou|gu|gua|guai|guan|guang|gui" "|gun|guo|ha|hai|han|hang|hao|he|hei|hen|heng|hong|hou|hu|hua|huai|huan|huang|hui|hun|huo|ji|jia|jian|jiang" "|jiao|jie|jin|jing|jiong|jiu|ju|juan|jue|jun|ka|kai|kan|kang|kao|ke|ken|keng|kong|kou|ku|kua|kuai|kuan|kuang" "|kui|kun|kuo|la|lai|lan|lang|lao|le|lei|leng|li|lia|lian|liang|liao|lie|lin|ling|liu|long|lou|lu|lv|luan|lue" "|lun|luo|ma|mai|man|mang|mao|me|mei|men|meng|mi|mian|miao|mie|min|ming|miu|mo|mou|mu|na|nai|nan|nang|nao|ne" "|nei|nen|neng|ni|nian|niang|niao|nie|nin|ning|niu|nong|nu|nv|nuan|nue|nuo|o|ou|pa|pai|pan|pang|pao|pei|pen" "|peng|pi|pian|piao|pie|pin|ping|po|pu|qi|qia|qian|qiang|qiao|qie|qin|qing|qiong|qiu|qu|quan|que|qun|ran|rang" "|rao|re|ren|reng|ri|rong|rou|ru|ruan|rui|run|ruo|sa|sai|san|sang|sao|se|sen|seng|sha|shai|shan|shang|shao|" "she|shen|sheng|shi|shou|shu|shua|shuai|shuan|shuang|shui|shun|shuo|si|song|sou|su|suan|sui|sun|suo|ta|tai|" "tan|tang|tao|te|teng|ti|tian|tiao|tie|ting|tong|tou|tu|tuan|tui|tun|tuo|wa|wai|wan|wang|wei|wen|weng|wo|wu" "|xi|xia|xian|xiang|xiao|xie|xin|xing|xiong|xiu|xu|xuan|xue|xun|ya|yan|yang|yao|ye|yi|yin|ying|yo|yong|you" "|yu|yuan|yue|yun|za|zai|zan|zang|zao|ze|zei|zen|zeng|zha|zhai|zhan|zhang|zhao|zhe|zhen|zheng|zhi|zhong|" "zhou|zhu|zhua|zhuai|zhuan|zhuang|zhui|zhun|zhuo|zi|zong|zou|zu|zuan|zui|zun|zuo"; $_DataValue = "-20319|-20317|-20304|-20295|-20292|-20283|-20265|-20257|-20242|-20230|-20051|-20036|-20032|-20026|-20002|-19990" "|-19986|-19982|-19976|-19805|-19784|-19775|-19774|-19763|-19756|-19751|-19746|-19741|-19739|-19728|-19725" "|-19715|-19540|-19531|-19525|-19515|-19500|-19484|-19479|-19467|-19289|-19288|-19281|-19275|-19270|-19263" "|-19261|-19249|-19243|-19242|-19238|-19235|-19227|-19224|-19218|-19212|-19038|-19023|-19018|-19006|-19003" "|-18996|-18977|-18961|-18952|-18783|-18774|-18773|-18763|-18756|-18741|-18735|-18731|-18722|-18710|-18697" "|-18696|-18526|-18518|-18501|-18490|-18478|-18463|-18448|-18447|-18446|-18239|-18237|-18231|-18220|-18211" "|-18201|-18184|-18183|-18181|-18012|-17997|-17988|-17970|-17964|-17961|-17950|-17947|-17931|-17928|-17922" "|-17759|-17752|-17733|-17730|-17721|-17703|-17701|-17697|-17692|-17683|-17676|-17496|-17487|-17482|-17468" "|-17454|-17433|-17427|-17417|-17202|-17185|-16983|-16970|-16942|-16915|-16733|-16708|-16706|-16689|-16664" "|-16657|-16647|-16474|-16470|-16465|-16459|-16452|-16448|-16433|-16429|-16427|-16423|-16419|-16412|-16407" "|-16403|-16401|-16393|-16220|-16216|-16212|-16205|-16202|-16187|-16180|-16171|-16169|-16158|-16155|-15959" "|-15958|-15944|-15933|-15920|-15915|-15903|-15889|-15878|-15707|-15701|-15681|-15667|-15661|-15659|-15652" "|-15640|-15631|-15625|-15454|-15448|-15436|-15435|-15419|-15416|-15408|-15394|-15385|-15377|-15375|-15369" "|-15363|-15362|-15183|-15180|-15165|-15158|-15153|-15150|-15149|-15144|-15143|-15141|-15140|-15139|-15128" "|-15121|-15119|-15117|-15110|-15109|-14941|-14937|-14933|-14930|-14929|-14928|-14926|-14922|-14921|-14914" "|-14908|-14902|-14894|-14889|-14882|-14873|-14871|-14857|-14678|-14674|-14670|-14668|-14663|-14654|-14645" "|-14630|-14594|-14429|-14407|-14399|-14384|-14379|-14368|-14355|-14353|-14345|-14170|-14159|-14151|-14149" "|-14145|-14140|-14137|-14135|-14125|-14123|-14122|-14112|-14109|-14099|-14097|-14094|-14092|-14090|-14087" "|-14083|-13917|-13914|-13910|-13907|-13906|-13905|-13896|-13894|-13878|-13870|-13859|-13847|-13831|-13658" "|-13611|-13601|-13406|-13404|-13400|-13398|-13395|-13391|-13387|-13383|-13367|-13359|-13356|-13343|-13340" "|-13329|-13326|-13318|-13147|-13138|-13120|-13107|-13096|-13095|-13091|-13076|-13068|-13063|-13060|-12888" "|-12875|-12871|-12860|-12858|-12852|-12849|-12838|-12831|-12829|-12812|-12802|-12607|-12597|-12594|-12585" "|-12556|-12359|-12346|-12320|-12300|-12120|-12099|-12089|-12074|-12067|-12058|-12039|-11867|-11861|-11847" "|-11831|-11798|-11781|-11604|-11589|-11536|-11358|-11340|-11339|-11324|-11303|-11097|-11077|-11067|-11055" "|-11052|-11045|-11041|-11038|-11024|-11020|-11019|-11018|-11014|-10838|-10832|-10815|-10800|-10790|-10780" "|-10764|-10587|-10544|-10533|-10519|-10331|-10329|-10328|-10322|-10315|-10309|-10307|-10296|-10281|-10274" "|-10270|-10262|-10260|-10256|-10254";

$_TDataKey = explode('|', $_DataKey);

$_TDataValue = explode('|', $_DataValue);

$_Data = (PHP_VERSION>='50') array_combine($_TDataKey, $_TDataValue) : _Array_Combine($_TDataKey, $_TDataValue);

arsort($_Data);

reset($_Data);

if($_Code != 'gb2312') $_String = _U2_Utf8_gb($_String);

$_Res = '';

for($i=0;$i<strlen($_String);$i++){

$_P = ord(substr($_String, $i, 1));

if($_P>160){

$_Q = ord(substr($_String, ++$i, 1));

$_P = $_P256 + $_Q - 65536;

}

$_Res =_Pinyin($_P, $_Data);

}

$cls_str=array("!","@","#","$","%","^","&","","(",")","~","<",">","{","}","[","]","",",","","/","\\","|","+"," ");

return str_replace($cls_str,"",$_Res);//preg_replace("/[^a-z0-9]/", '', $_Res);

}

function _Pinyin($_Num, $_Data) {

if ($_Num>0 && $_Num<160 ) return chr($_Num);

elseif($_Num<-20319 || $_Num>-10247) return '';

else {

foreach($_Data as $k=>$v){ if($v<=$_Num) break;

} // return "_"$k;

return $k;

}

} function _U2_Utf8_gb($_C) {

$_String = '';

if($_C < 0x80) $_String = $_C;

elseif($_C < 0x800) {

$_String = chr(0xC0 | $_C>>6);

$_String = chr(0x80 | $_C & 0x3F);

}elseif($_C < 0x10000){

$_String = chr(0xE0 | $_C>>12);

$_String = chr(0x80 | $_C>>6 & 0x3F);

$_String = chr(0x80 | $_C & 0x3F);

} elseif($_C < 0x200000) {

$_String = chr(0xF0 | $_C>>18);

$_String = chr(0x80 | $_C>>12 & 0x3F);

$_String = chr(0x80 | $_C>>6 & 0x3F);

$_String = chr(0x80 | $_C & 0x3F);

} return iconv('UTF-8', 'gb2312', $_String);

}

function _Array_Combine($_Arr1, $_Arr2) {

for($i=0;$i<count($_Arr1);$i++) $_Res[$_Arr1[$i]] = $_Arr2[$i];

return $_Res;

}

echo Pinyin('汉字转拼音测试');

//默认是gb编码

>

欢迎分享,转载请注明来源:品搜搜测评网

原文地址:https://pinsoso.cn/meirong/2579416.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-12-14
下一篇2023-12-14

随机推荐

  • 莱珀妮鱼子酱精华真假

    莱珀妮鱼子酱精华是一款非常受欢迎的美容产品,它所使用的成分包括鲟鱼子和珍珠等天然提取物,被誉为能够改善肌肤质量、提升肌肤光泽度的神奇产品。对于这款产品,关心它真假问题的人们很多,我们从以下几个角度来回答大家的疑问。我们需要知道莱珀妮鱼子酱精

    2024-04-15
    48700
  • 薇诺娜清痘修复精华液好用吗?薇诺娜清痘精华液祛痘效果

    脸上长痘痘这样的肌肤问题,在现实生活中是非常常见的,也是很多年轻肌肤的美眉们最大的肌肤问题,长痘痘会使自己的肌肤有炎症损伤,并且还会导致肌肤的毛孔粗大,今天小编要介绍的是一款祛痘的精华液,是纯植物配方的,就是薇诺娜清痘修复精华液。1、产品介

    2024-04-15
    44400
  • 黑松露能使白发变黑吗 黑松露能治白发吗

    黑松露大家应该都不陌生,它是一种很珍贵的食材,其实有不少人应该都有吃过的,有着极高的营养价值,那么经常吃黑松露能使白发变黑吗?黑松露能使白发变黑吗不能,黑松露没有这个作用。黑松露含有的多糖、多肽、三萜具有增强免疫力、抗衰老、抗疲劳等

    2024-04-15
    38100
  • 提拉紧致脸部的护肤品

    提拉紧致脸部的护肤品如下:1、海蓝之谜倍添紧致露海蓝之谜的大部分产品主要是修复的,而这款精华是其罕见的抗衰老产品。它添加的成分是深海的活性精华,其中所含的蓝藻蓝铜宝石是提升和收紧皮肤的重要来源。它能再生面部胶原蛋白,使其更具弹性和活力。2、

    2024-04-15
    36100
  • 口碑最好十大沐浴露

    口碑最好十大沐浴露有:多芬、强生婴儿、力士、舒肤佳、玉兰油、六神、妮维雅、凌仕、威露士、阿迪达斯。1、多芬:多芬创立于1957年,是全球著名的女性品牌,隶属于联合利华旗下。多芬是秀发和肌肤理护专家,主要产品是沐浴乳、洗发水及护发素。2、强生

    2024-04-15
    35700
  • 保湿露保湿霜和保湿精华有区别吗?

    保湿露、保湿霜和保湿精华有区别:1、含油和水的比例不同:保湿露和保湿精华的水分比较多,保湿霜霜类产品含油脂多些。2、功效不同:保湿露起补水功效,保湿霜功效是锁水,保湿精华兼具补水和锁水功效。3、质地不同:保湿露质地较为清爽,保湿霜质地会更厚

    2024-04-15
    35500
  • MZC新肌修护精华液与精华油有什么区别吗?

    MZC新肌修护精华液和精华油的区别MZC新肌修护精华液和精华油都是护肤品中的一种,它们的作用都是滋润和修复肌肤。但是,它们的成分和使用方法有所不同。首先从成分上看,精华液的主要成分是水,它在质地上更加清爽轻盈,容易被皮肤吸收。而花梦花精华油

    2024-04-15
    35800

发表评论

登录后才能评论
保存