Cml\Vendor\Pingyin::pinyin PHP Method

pinyin() public static method

中文转拼音
public static pinyin ( $string, boolean $utf8 = true ) : string
$string
$utf8 boolean
return string
    public static function pinyin($string, $utf8 = true)
    {
        $string = $utf8 === true ? iconv('utf-8', 'gbk', $string) : $string;
        if (self::$pinyinArr == null) {
            self::$pinyinArr = self::pinyinCode();
        }
        $num = strlen($string);
        $pinyin = '';
        for ($i = 0; $i < $num; $i++) {
            $temp = ord(substr($string, $i, 1));
            if ($temp > 160) {
                $temp2 = ord(substr($string, ++$i, 1));
                $temp = $temp * 256 + $temp2 - 65536;
            }
            if ($temp > 0 && $temp < 160) {
                $pinyin .= chr($temp);
            } elseif ($temp < -20319 || $temp > -10247) {
                $pinyin .= '';
            } else {
                $total = sizeof(self::$pinyinArr) - 1;
                for ($j = $total; $j >= 0; $j--) {
                    if (self::$pinyinArr[$j][1] <= $temp) {
                        break;
                    }
                }
                $pinyin .= self::$pinyinArr[$j][0];
            }
        }
        return $utf8 == true ? iconv('gbk', 'utf-8', $pinyin) : $pinyin;
    }