• 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);

  • 相关阅读:
    对于捐赠承诺和劳务捐赠,不予以确认,但应在会计报表附注中披露
    R语言代写线性混合效应模型Linear Mixed-Effects Models的部分折叠Gibbs采样
    matlab代写MCMC贝叶斯方法用于加筋复合板的冲击载荷识别
    R语言代写dplyr-高效的数据变换与整理工具
    GIS代写遥感数据可视化评估:印度河流域上部的积雪面积变化
    R语言代写向量自回归模型(VAR)及其实现
    r语言代写实现似然的I(2)协整VAR模型弱外生性推理
    python代写缺失值处理案例分析:泰坦尼克数据
    Python代写高性能计算库——Numba
    matlab递归神经网络RNN实现:桨距控制控制风力发电机组研究
  • 原文地址:https://www.cnblogs.com/jackspider/p/2976336.html
Copyright © 2020-2023  润新知