使用PHP获取汉字的拼音

使用PHP获取汉字的拼音,第1张

复制代码 代码如下: <php class GetPingYing { private $pylist = array( 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 => ); //全部拼音 public function getAllPY($chinese $delimiter = $length = ) { $py = $this >zh_to_pys($chinese $delimiter); if($length) { $py = substr($py $length); } return $py; } //拼音首个字母 public function getFirstPY($chinese){ $result = ; for ($i= ; $i<strlen($chinese); $i++) { $p = ord(substr($chinese $i )); if ($p> ) { $q = ord(substr($chinese ++$i )); $p = $p + $q ; } $result = substr($this >zh_to_py($p) ); } return $result ; } // 中文转拼音 // private function zh_to_py($num $blank = ) { if($num> && $num< ) { return chr($num); } elseif ($num< ||$num> ) { return $blank; } else { foreach ($this >pylist as $py => $code) { if($code > $num) break; $result = $py; } return $result; } } private function zh_to_pys($chinese $delimiter = $first= ){ $result = array(); for($i= ; $i<strlen($chinese); $i++) { $p = ord(substr($chinese $i )); if($p> ) { $q = ord(substr($chinese ++$i )); $p = $p + $q ; } $result[] = $this >zh_to_py($p); if ($first) { return $result[ ]; } } return implode($delimiter $result); } } // 中文转拼音结束 // //中文是双字节 所以需要两个字节连接起来(ASCII码的范围是在 ) $c = 齐秦 ; for($i= ; $i<strlen($c); $i++) { echo ord($c[$i]) ;// } echo <br> chr( ) chr( ) chr( ) chr( ) <br> ; //测试 $PingYing = new GetPingYing(); echo <br> $PingYing >getFirstPY( 羽泉乐队 歌不错 推荐 ) <br> ;//yqldgbc tj echo $PingYing >getAllPY( 羽泉乐队 歌不错 推荐 ) <br> ; //yuquanleduigebucuo tuijian > lishixinzhi/Article/program/PHP/201311/21313

这个问题只有通过查询拼音和汉字的映射表来实现。这种映射表在网上可以下得到。不过要看一下,有一些网上的表是不完整的。

有了映射表后就要对拼音按字母进行索引存储,在你输入拼音的时候就是基于这个索引映射表来查找对应的汉字,如果有重码就要提示用户选择。

以上就是关于使用PHP获取汉字的拼音全部的内容,包括:使用PHP获取汉字的拼音、怎样在ASP.Net(C#)中获得汉字的全拼音、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9564341.html

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

发表评论

登录后才能评论

评论列表(0条)

保存