laravel 将汉字转化成拼音的库

时间:2021-11-05 14:51:08

第一步:composer require "overtrue/laravel-pinyin:~3.0"

第二步:配置

1,Add the following line to the section providers of config/app.php:在配置文件config/app.php配置一下参数

'providers' => [  //...  Overtrue\LaravelPinyin\ServiceProvider::class, ],

2,as optional, you can use facade:你也可以使用别名
'aliases' => [  //...  'Pinyin' => Overtrue\LaravelPinyin\Facades\Pinyin::class, ],
相关的配置消息,你也可以看连接:https://packagist.org/packages/overtrue/laravel-pinyin

for example
 1   /**
 2      * 拼音的扩展包使用
 3      * @param Request $request
 4      * @param Pinyin $pinyin
 5      * @return string
 6      */
 7     public function testOfPinyin(Request $request,Pinyin $pinyin)
 8     {
 9         $params = $this->validate($request,[
10            'name'    =>  'required',
11         ]);
12 //        $pinyin = new Pinyin();
13         $name_spell = $pinyin->convert($params['name']);
14         $name_initial = $pinyin->abbr($params['name']);
15         $name_spell = strtoupper(implode('',$name_spell));//转为字符串,并且转为大写 XIAZHAOJIANTANGPEI
16         $name_initial = strtoupper($name_initial);//转为大写   XZJTP
17 
18 //        $name1 = $pinyin->sentence($params['name']);//"xia zhao jian tang pei"
19 //        $name2 = $pinyin->permalink($params['name']);//"xia-zhao-jian-tang-pei"
20 //        var_dump($name1);
21         return $name_initial.'------'.$name_spell;
22     }