Bootstrap

PHP中文转拼音

导入第三方库

composer require "overtrue/pinyin:~4.0"

函数

    /**
     * 中文转拼音
     * array(2) {
        ["py"] => string(3) "fff"
        ["pinyin"] => string(9) "feifeifei"
        }
     */
    function pinYin($str)
    {
        vendor ('PinYin.PinYin');
        $pinyin = new \Overtrue\Pinyin\Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
        // $v['Pinyin'] = $str;
        $row['py'] = $pinyin->abbr($str, '');//拼音首字母
        $row['pinyin'] = $pinyin->permalink($str, '');//全拼
        return $row;
    }

pinYin('飞飛飝');

如果是fastadmin框架的话,可以直接使用。

use fast\Pinyin;
Pinyin::get('飞飛飝');

;