php获得汉字拼音首字母
发布时间:2022-07-29, 09:15:29 分类:PHP | 编辑 off 网址 | 辅助
图集1/2
正文 1815字数 316,284阅读
在php中将汉字转为拼音需要使用“overtrue/pinyin”拓展;
获得汉字拼音首字母的方法:首先使用“new Pinyin()”创建对象并赋值给“$pinyin”;
然后使用“$pinyin->abbr();”来将汉字转成拼音数组。
php汉字转拼音需要使用overtrue/pinyin拓展
一:安装overtrue/pinyin拓展
composer require overtrue/pinyin
Run code
Cut to clipboard
overtrue/pinyin拓展GitHub地址
二:overtrue/pinyin拓展简单使用
overtrue/pinyin拼音转成拼音常见的转化方式:
PINYIN_TONE #带音调的转化方式
PINYIN_KEEP_NUMBER #保留数字的转化方式
PINYIN_KEEP_ENGLISH#保留英文的转化方式
PINYIN_KEEP_PUNCTUATION#保留标点的转化方式
PINYIN_UMLAUT_V#使用 v 代替 yu的转化方式
Run code
Cut to clipboard
1:汉字转成拼音数组
(1):汉字转成无无音调的拼音
$pinyin = new Pinyin();
$pinyin->convert('测试汉字转拼音');
Run code
Cut to clipboard
输出数据为:
['ce', 'shi', 'han', 'zi', 'zhuan', 'pin', 'yin']
(2):汉字转成带音调的拼音
$pinyin = new Pinyin();
$pinyin->convert('测试汉字转拼音', PINYIN_TONE);
Run code
Cut to clipboard
输出数据为:
['cè', 'shì', 'hàn', 'zì', 'zhuǎn', 'pīn', 'yīn']
(3):汉字转拼音中lv拼音为lyu,使用此方法使用 v 代替 yu
$pinyin = new Pinyin();
$pinyin->convert('吕氏春秋');
$pinyin->convert('吕氏春秋', PINYIN_UMLAUT_V);
Run code
Cut to clipboard
第一个方法输出为:
['lyu', 'shi', 'chun', 'qiu']
第二个方法输出为:
['lv', 'shi', 'chun', 'qiu']
2:汉字转成拼音字符串
$pinyin = new Pinyin();
$pinyin->permalink('测试汉字转拼音');
$pinyin->permalink('测试汉字转拼音', '.');
Run code
Cut to clipboard
第一个方法输出为:
ce-shi-han-zi-zhuan-pin-yin
第二个方法输出为:
ce.shi.han.zi.zhuan.pin.yi
3:汉字转成首字母字符串
$pinyin = new Pinyin();
$pinyin->abbr('测试汉字转拼音');
$pinyin->abbr('测试汉字转拼音', '-');
Run code
Cut to clipboard
第一个方法输出为:
cshzzpy
第二个方法输出为:
c-s-h-z-z-p-y
4:整段汉字(带标点符号)转成拼音
$pinyin = new Pinyin();
$pinyin->sentence('测试汉字转拼音,这是测试!!!');
$pinyin->sentence('测试汉字转拼音,这是测试!!!', '-');
Run code
Cut to clipboard
第一个方法输出为:
ce shi han zi zhuan pin yin, zhe shi ce shi!!!
第二个方法输出为:
ce-shi-han-zi-zhuan-pin-yin,-zhe-shi-ce-shi!!!
5:姓名汉字转拼音
有些汉字常见读音和作为姓名读音不同,例如仇,常见读音为chou,但是作为姓氏时都qiu
$pinyin = new Pinyin();
$pinyin->name('仇某某');
Run code
Cut to clipboard
输出结果为:
['qiu', 'mou', 'mou']
(支付宝)给作者钱财以资鼓励 (微信)→
有过 4 条评论 »
//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',$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; } echo getFirstCharter('张');
下载拓展文件,放在extend目录下
控制器中使用
引入拓展文件:use topinyin\ChinesePinyin;
public function indexOp() { $Pinyin = new ChinesePinyin(); //带声调的汉语拼音 echo '我们的歌'; dump($Pinyin->TransformWithTone("我们的歌")); //无声调的汉语拼音 echo '我们的歌'; dump($Pinyin->TransformWithoutTone("我们的歌")); //转成汉语拼音首字母,只包括汉字,不包含拼音 echo '首字母只包括汉字BuHanPinYin'; dump($Pinyin->TransformUcwordsOnlyChar("首字母只包括汉字BuHanPinYin")); //转成汉语拼音首字母,包含非汉字内容 echo '首字母和其他字符如B区32号'; dump($Pinyin->TransformUcwords("首字母和其他字符如B区32号")); }
$root_path = \think\facade\App::getRootPath(); require_once $root_path . 'extend/Wxpay/WxPay.Config.php';
获取字符串汉字首字母,兼容GBK和UTF-8
<?php //获取单个汉字拼音首字母。注意:此处不要纠结。汉字拼音是没有以U和V开头的 function getfirstchar($s0){ $fchar = ord($s0{0}); if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($s0{0}); $s1 = iconv("UTF-8","gb2312", $s0); $s2 = iconv("gb2312","UTF-8", $s1); 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 >= -17922 and $asc <= -17418) return "I"; 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; //return $s0; } function pinyin_long($zh){ //获取整条字符串所有汉字拼音首字母 $ret = ""; $s1 = iconv("UTF-8","gb2312", $zh); $s2 = iconv("gb2312","UTF-8", $s1); if($s2 == $zh){$zh = $s1;} for($i = 0; $i < strlen($zh); $i++){ $s1 = substr($zh,$i,1); $p = ord($s1); if($p > 160){ $s2 = substr($zh,$i++,2); $ret .= getfirstchar($s2); }else{ $ret .= $s1; } } return $ret; } echo pinyin_long('*《,@#$123HAHadf一年后而已');echo "<br>";//console(*@#$123HAHadfYNHEY) function pinyin_long2($zh){ //获取整条字符串(非纯汉字链)首个汉字拼音首字母 $ret = ""; $s1 = iconv("UTF-8","gb2312", $zh); $s2 = iconv("gb2312","UTF-8", $s1); if($s2 == $zh){$zh = $s1;} for($i = 0; $i < strlen($zh); $i++){ $s1 = substr($zh,$i,1); $p = ord($s1); if($p > 160){ $s2 = substr($zh,$i++,2); $ret = getfirstchar($s2); }else{ $ret = $s1; } } return $ret; } echo pinyin_long2('*《,@#$123HAHadf一年后而已');//console(Y) ?>