zoukankan      html  css  js  c++  java
  • PHP 汉字 转换成 拼音

    这个程序是从 织梦里面 弄出来的 需要 拷贝 织梦里面的2个文件

    include/data/gb2312-utf8.dat

    include/data/pinyin.dat

    PHP 的 代码如下

    <?php
    /**
     *  UTF-8 转GB编码
     *
     * @access    public
     * @param     string  $utfstr  需要转换的字符串
     * @return    string
     */
    function utf82gb($utfstr)
        {
            if(function_exists('iconv'))
            {
                return iconv('utf-8','gbk//ignore',$utfstr);
            }
            global $UC2GBTABLE;
            $okstr = "";
            if(trim($utfstr)=="")
            {
                return $utfstr;
            }
            if(empty($UC2GBTABLE))
            {
                $filename = "./gb2312-utf8.dat";
                $fp = fopen($filename,"r");
                while($l = fgets($fp,15))
                {
                    $UC2GBTABLE[hexdec(substr($l, 7, 6))] = hexdec(substr($l, 0, 6));
                }
                fclose($fp);
            }
            $okstr = "";
            $ulen = strlen($utfstr);
            for($i=0;$i<$ulen;$i++)
            {
                $c = $utfstr[$i];
                $cb = decbin(ord($utfstr[$i]));
                if(strlen($cb)==8)
                {
                    $csize = strpos(decbin(ord($cb)),"0");
                    for($j=0;$j < $csize;$j++)
                    {
                        $i++; $c .= $utfstr[$i];
                    }
                    $c = utf82u($c);
                    if(isset($UC2GBTABLE[$c]))
                    {
                        $c = dechex($UC2GBTABLE[$c]+0x8080);
                        $okstr .= chr(hexdec($c[0].$c[1])).chr(hexdec($c[2].$c[3]));
                    }
                    else
                    {
                        $okstr .= "&#".$c.";";
                    }
                }
                else
                {
                    $okstr .= $c;
                }
            }
            $okstr = trim($okstr);
            return $okstr;
        }

    /**
     *  获取拼音信息
     *
     * @access    public
     * @param     string  $str  字符串
     * @param     int  $ishead  是否为首字母
     * @param     int  $isclose  解析后是否释放资源
     * @return    string
     */
    function SpGetPinyin($str, $ishead=0, $isclose=1)
    {
        global $pinyins;
        $restr = '';
        $str = trim($str);
        $slen = strlen($str);
        if($slen < 2)
        {
            return $str;
        }
        if(count($pinyins) == 0)
        {
            $fp = fopen('./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 .= "_";
            }
        }
        if($isclose==0)
        {
            unset($pinyins);
        }
        return $restr;
    }


    /**
     *  获取拼音以gbk编码为准
     *
     * @access    public
     * @param     string  $str     字符串信息
     * @param     int     $ishead  是否取头字母
     * @param     int     $isclose 是否关闭字符串资源
     * @return    string
     */
    function GetPinyin($str, $ishead=0, $isclose=1){
        return SpGetPinyin(utf82gb($str), $ishead, $isclose);
            
    }

    $str = '回到乱世建山寨';
    echo GetPinyin($str);

  • 相关阅读:
    Statement
    打印页数设定
    点选TOP后并不是直接跳到页顶的,而是滚动上去
    文本框不允许输入特殊字符,只能是数字、字母、-和_,不允许输入空格键
    不间断滚动
    无限级别的菜单(侧拉菜单)
    筛法求素数
    1212
    触发器引发的entityframework异常
    using crystalreport generate PDF2
  • 原文地址:https://www.cnblogs.com/jackspider/p/2976336.html
Copyright © 2011-2022 走看看