PHP中汉字转全拼音或首字母

// +----------------------------------------------------------------------

// | email [ iActing ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018~2019 http://www.ijiandian.com All rights reserved.
// +----------------------------------------------------------------------
// | Data: 2019/3/16
// +----------------------------------------------------------------------
// | Author: iActing <758246061@qq.com>
// +----------------------------------------------------------------------
class Demo
{

/**
 *  获取拼音以gbk编码为准
 *
 * @method    汉字转全拼音/首字母
 * @param     string $str  字符串信息
 * @param     int $ishead  是否取头字母
 * @param     int $isclose 是否关闭字符串资源
 * @return    string
 */

function getPinyin($str, $ishead = 0, $isclose = 1, $lang = 'utf-8')
{
    if ($lang == 'utf-8') {
        return $this->_getPinyin($this->utf82gb($str), $ishead, $isclose);
    } else {
        return $this->_getPinyin($str, $ishead, $isclose);
    }
}

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

}

/**
 * @method  UTF-8   转GB编码
 * @param   $utfstr 需要转换的字符串
 * @return  string
 */
function utf82gb($utfstr)
{
    if (function_exists('iconv')) {
        return iconv('utf-8', 'gbk//ignore', $utfstr);
    }
    global $UC2GBTABLE;
    if (trim($utfstr) == "") {
        return $utfstr;
    }
    if (empty($UC2GBTABLE)) {
        $filename = "./dat/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;
}
}

// 调用演示

$test = new Demo();
$res = $test->getPinyin('你'); // 返回 ni
$res = $test->getPinyin('你好',1); // 返回 nh

点击下载附件→ 点击下载附件

Last modification:March 24th, 2019 at 09:32 pm
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment