heyuntao
2023-10-07 f2528a40b631dd4165484b9fce6b5c2708aae5c7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
 
namespace fast;
 
/**
 * 中文转拼音类
 */
class Pinyin
{
 
    /**
     * 获取文字的拼音
     * @param string  $chinese   中文汉字
     * @param boolean $onlyfirst 是否只返回拼音首字母
     * @param string  $delimiter 分隔符
     * @param bool    $ucfirst   是否首字母大写
     * @return string
     */
    public static function get($chinese, $onlyfirst = false, $delimiter = '', $ucfirst = false)
    {
 
        $pinyin = new \Overtrue\Pinyin\Pinyin();
        if ($onlyfirst) {
            $result = $pinyin->abbr($chinese, $delimiter);
        } else {
            $result = $pinyin->permalink($chinese, $delimiter);
        }
        if ($ucfirst) {
            $pinyinArr = explode($delimiter, $result);
            $result = implode($delimiter, array_map('ucfirst', $pinyinArr));
        }
 
        return $result;
    }
 
}