1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace tinymeng\tools; |
4
|
|
|
/** |
5
|
|
|
* Name: Doc.php. |
6
|
|
|
* Author: JiaMeng <[email protected]> |
7
|
|
|
* Date: 2017/8/20 17:20 |
8
|
|
|
* Description: Doc.php. |
9
|
|
|
*/ |
10
|
|
|
class ChineseChar |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
static public function getArray() |
14
|
|
|
{ |
15
|
|
|
return unserialize(file_get_contents(__DIR__.'/lib/chinese-char-library.txt')); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Description: 获取字符串的首字母 |
20
|
|
|
* Author: JiaMeng <[email protected]> |
21
|
|
|
* Updater: |
22
|
|
|
* @param string $string 要转换的字符串 |
23
|
|
|
* @param bool $isOne 是否取首字母 |
24
|
|
|
* @param bool $upper 是否转换为大写 |
25
|
|
|
* @return bool|mixed|string |
26
|
|
|
* |
27
|
|
|
* 例如:getChineseFirstChar('我是作者') 首字符全部字母+小写 |
28
|
|
|
* return "wo" |
29
|
|
|
* |
30
|
|
|
* 例如:getChineseFirstChar('我是作者',true) 首字符首字母+小写 |
31
|
|
|
* return "w" |
32
|
|
|
* |
33
|
|
|
* 例如:getChineseFirstChar('我是作者',true,true) 首字符首字母+大写 |
34
|
|
|
* return "W" |
35
|
|
|
* |
36
|
|
|
* 例如:getChineseFirstChar('我是作者',false,true) 首字符全部字母+大写 |
37
|
|
|
* return "WO" |
38
|
|
|
*/ |
39
|
|
|
static public function getChineseFirstChar($string, $isOne = false, $upper = false) |
40
|
|
|
{ |
41
|
|
|
$spellArray = self::getArray(); |
42
|
|
|
$str_arr = self::utf8StrSplit($string, 1); //将字符串拆分成数组 |
43
|
|
|
|
44
|
|
|
if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $str_arr[0])) { //判断是否是汉字 |
45
|
|
|
$chinese = $spellArray[$str_arr[0]]; |
46
|
|
|
$result = $chinese[0]; |
47
|
|
|
} else { |
48
|
|
|
$result = $str_arr[0]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$result = $isOne ? substr($result, 0, 1) : $result; |
52
|
|
|
|
53
|
|
|
return $upper ? strtoupper($result) : $result; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Description: 将字符串转换成拼音字符串 |
58
|
|
|
* Author: JiaMeng <[email protected]> |
59
|
|
|
* Updater: |
60
|
|
|
* @param string $string 汉字字符串 |
61
|
|
|
* @param bool $isOne 是否大写 |
62
|
|
|
* @param bool $upper 是否大写 |
63
|
|
|
* @return string |
64
|
|
|
* 例如:getChineseChar('我是作者'); 全部字符串+小写 |
65
|
|
|
* return "wo shi zuo zhe" |
66
|
|
|
* |
67
|
|
|
* 例如:getChineseChar('我是作者',true); 首字母+小写 |
68
|
|
|
* return "w s z z" |
69
|
|
|
* |
70
|
|
|
* 例如:getChineseChar('我是作者',true,true); 首字母+大写 |
71
|
|
|
* return "W S Z Z" |
72
|
|
|
* |
73
|
|
|
* 例如:getChineseChar('我是作者',false,true); 首字母+大写 |
74
|
|
|
* return "WO SHI ZUO ZHE" |
75
|
|
|
*/ |
76
|
|
|
static public function getChineseChar($string, $isOne = false, $upper = false) |
77
|
|
|
{ |
78
|
|
|
$spellArray = self::getArray(); |
79
|
|
|
$str_arr = self::utf8StrSplit($string, 1); //将字符串拆分成数组 |
80
|
|
|
$result = array(); |
81
|
|
|
foreach ($str_arr as $char) { |
82
|
|
|
if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $char)) { |
83
|
|
|
$chinese = $spellArray[$char]; |
84
|
|
|
$chinese = $chinese[0]; |
85
|
|
|
} else { |
86
|
|
|
$chinese = $char; |
87
|
|
|
} |
88
|
|
|
$chinese = $isOne ? substr($chinese, 0, 1) : $chinese; |
89
|
|
|
$result[] = $upper ? strtoupper($chinese) : $chinese; |
90
|
|
|
} |
91
|
|
|
return implode(' ', $result); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Description: 将字符串转换成数组 |
96
|
|
|
* Author: JiaMeng <[email protected]> |
97
|
|
|
* Updater: |
98
|
|
|
* @param string $str 要转换的字符串 |
99
|
|
|
* @param int $split_len |
100
|
|
|
* @return array|bool |
101
|
|
|
*/ |
102
|
|
|
private static function utf8StrSplit($str, $split_len = 1) |
103
|
|
|
{ |
104
|
|
|
if (!preg_match('/^[0-9]+$/', $split_len) || $split_len < 1) { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$len = mb_strlen($str, 'UTF-8'); |
109
|
|
|
|
110
|
|
|
if ($len <= $split_len) { |
111
|
|
|
return array($str); |
112
|
|
|
} |
113
|
|
|
preg_match_all('/.{' . $split_len . '}|[^\x00]{1,' . $split_len . '}$/us', $str, $ar); |
114
|
|
|
|
115
|
|
|
return $ar[0]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |