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