«
用php实现把汉字转化为拼音的一个函数

时间:2008-5-31    作者:Deri    分类: 分享


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