龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php汉字转拼音(dede)

时间:2014-07-22 14:48来源: 作者: 点击:
分享到:
php汉字转拼音(dede)拉个外链哈!http://www.tao11.cn/a0b923820dcc509a.htmlhttp://www.tao11.cn/9d4c2f636f067f89.htmlhttp://www.tao11.cn/4b5ce2fe28308fd9.htmlhttp://www.tao11.cn/bbce2345d7772b06.html里面的pinyin.dat 在附近里面
php汉字转拼音(dede)
拉个外链哈!
http://www.tao11.cn/a0b923820dcc509a.html
http://www.tao11.cn/9d4c2f636f067f89.html
http://www.tao11.cn/4b5ce2fe28308fd9.html
http://www.tao11.cn/bbce2345d7772b06.html
里面的pinyin.dat 在附近里面
		/**
		 * 汉字
		 * @param string $str 待转换的字符串
		 * @param string $charset 字符串编码
		 * @param bool $ishead 是否只提取首字母
		 * @return string 返回结果
		 */
		static function GetPinyin($str,$charset="utf-8",$ishead = 0) {
		    $restr = '';
		    $str = trim($str);
		    if($charset=="utf-8"){
		        $str=iconv("utf-8","gb2312",$str);
		    }
		    $slen = strlen($str);
		    $pinyins=array();
		    if ($slen < 2) {
		        return $str;
		    }
		    $fp = fopen(dirname(__FILE__).'/pinyin.dat', 'r');
		    while (!feof($fp)) {
		        $line = trim(fgets($fp));
		        $pinyins[$line[0] . $line[1]] = substr($line, 3, strlen($line) - 3);
		    }
		    fclose($fp);
		    
		    for ($i = 0; $i < $slen; $i++) {
		        if (ord($str[$i]) > 0x80) {
		            $c = $str[$i] . $str[$i + 1];
		            $i++;
		            if (isset($pinyins[$c])) {
		                if ($ishead == 0) {
		                    $restr .= $pinyins[$c];
		                } else {
		                    $restr .= $pinyins[$c][0];
		                }
		            } else {
		                $restr .= "_";
		            }
		        } else if (preg_match("/[a-z0-9]/i", $str[$i])) {
		            $restr .= $str[$i];
		        } else {
		            $restr .= "_";
		        }
		    }
		    return $restr;
		}

2. [文件] pinyin.rar ~ 18KB     下载(11)     [全屏预览]

精彩图集

赞助商链接