1 | <?php |
||||
2 | |||||
3 | namespace tinymeng\Chinaums\Tools; |
||||
4 | |||||
5 | class Str |
||||
6 | { |
||||
7 | |||||
8 | protected static $snakeCache = []; |
||||
9 | |||||
10 | protected static $camelCache = []; |
||||
11 | |||||
12 | protected static $studlyCache = []; |
||||
13 | |||||
14 | /** |
||||
15 | * 检查字符串中是否包含某些字符串 |
||||
16 | * @param string $haystack |
||||
17 | * @param string|array $needles |
||||
18 | * @return bool |
||||
19 | */ |
||||
20 | public static function contains(string $haystack, $needles): bool |
||||
21 | { |
||||
22 | foreach ((array) $needles as $needle) { |
||||
23 | if ('' != $needle && mb_strpos($haystack, $needle) !== false) { |
||||
24 | return true; |
||||
25 | } |
||||
26 | } |
||||
27 | |||||
28 | return false; |
||||
29 | } |
||||
30 | |||||
31 | /** |
||||
32 | * 检查字符串是否以某些字符串结尾 |
||||
33 | * |
||||
34 | * @param string $haystack |
||||
35 | * @param string|array $needles |
||||
36 | * @return bool |
||||
37 | */ |
||||
38 | public static function endsWith(string $haystack, $needles): bool |
||||
39 | { |
||||
40 | foreach ((array) $needles as $needle) { |
||||
41 | if ((string) $needle === static::substr($haystack, -static::length($needle))) { |
||||
42 | return true; |
||||
43 | } |
||||
44 | } |
||||
45 | |||||
46 | return false; |
||||
47 | } |
||||
48 | |||||
49 | /** |
||||
50 | * 检查字符串是否以某些字符串开头 |
||||
51 | * |
||||
52 | * @param string $haystack |
||||
53 | * @param string|array $needles |
||||
54 | * @return bool |
||||
55 | */ |
||||
56 | public static function startsWith(string $haystack, $needles): bool |
||||
57 | { |
||||
58 | foreach ((array) $needles as $needle) { |
||||
59 | if ('' != $needle && mb_strpos($haystack, $needle) === 0) { |
||||
60 | return true; |
||||
61 | } |
||||
62 | } |
||||
63 | |||||
64 | return false; |
||||
65 | } |
||||
66 | |||||
67 | /** |
||||
68 | * 获取指定长度的随机字母数字组合的字符串 |
||||
69 | * |
||||
70 | * @param int $length |
||||
71 | * @param int $type |
||||
72 | * @param string $addChars |
||||
73 | * @return string |
||||
74 | */ |
||||
75 | public static function random(int $length = 6, int $type = null, string $addChars = ''): string |
||||
76 | { |
||||
77 | $str = ''; |
||||
78 | switch ($type) { |
||||
79 | case 0: |
||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||
80 | $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' . $addChars; |
||||
81 | break; |
||||
82 | case 1: |
||||
83 | $chars = str_repeat('0123456789', 3); |
||||
84 | break; |
||||
85 | case 2: |
||||
86 | $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . $addChars; |
||||
87 | break; |
||||
88 | case 3: |
||||
89 | $chars = 'abcdefghijklmnopqrstuvwxyz' . $addChars; |
||||
90 | break; |
||||
91 | case 4: |
||||
92 | $chars = "们以我到他会作时要动国产的一是工就年阶义发成部民可出能方进在了不和有大这主中人上为来分生对于学下级地个用同行面说种过命度革而多子后自社加小机也经力线本电高量长党得实家定深法表着水理化争现所二起政三好十战无农使性前等反体合斗路图把结第里正新开论之物从当两些还天资事队批点育重其思与间内去因件日利相由压员气业代全组数果期导平各基或月毛然如应形想制心样干都向变关问比展那它最及外没看治提五解系林者米群头意只明四道马认次文通但条较克又公孔领军流入接席位情运器并飞原油放立题质指建区验活众很教决特此常石强极土少已根共直团统式转别造切九你取西持总料连任志观调七么山程百报更见必真保热委手改管处己将修支识病象几先老光专什六型具示复安带每东增则完风回南广劳轮科北打积车计给节做务被整联步类集号列温装即毫知轴研单色坚据速防史拉世设达尔场织历花受求传口断况采精金界品判参层止边清至万确究书" . $addChars; |
||||
93 | break; |
||||
94 | default: |
||||
95 | $chars = 'ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789' . $addChars; |
||||
96 | break; |
||||
97 | } |
||||
98 | if ($length > 10) { |
||||
99 | $chars = $type == 1 ? str_repeat($chars, $length) : str_repeat($chars, 5); |
||||
100 | } |
||||
101 | if ($type != 4) { |
||||
102 | $chars = str_shuffle($chars); |
||||
103 | $str = substr($chars, 0, $length); |
||||
104 | } else { |
||||
105 | for ($i = 0; $i < $length; $i++) { |
||||
106 | $str .= mb_substr($chars, floor(mt_rand(0, mb_strlen($chars, 'utf-8') - 1)), 1); |
||||
0 ignored issues
–
show
floor(mt_rand(0, mb_strlen($chars, 'utf-8') - 1)) of type double is incompatible with the type integer expected by parameter $start of mb_substr() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
107 | } |
||||
108 | } |
||||
109 | return $str; |
||||
110 | } |
||||
111 | |||||
112 | /** |
||||
113 | * 字符串转小写 |
||||
114 | * |
||||
115 | * @param string $value |
||||
116 | * @return string |
||||
117 | */ |
||||
118 | public static function lower(string $value): string |
||||
119 | { |
||||
120 | return mb_strtolower($value, 'UTF-8'); |
||||
121 | } |
||||
122 | |||||
123 | /** |
||||
124 | * 字符串转大写 |
||||
125 | * |
||||
126 | * @param string $value |
||||
127 | * @return string |
||||
128 | */ |
||||
129 | public static function upper(string $value): string |
||||
130 | { |
||||
131 | return mb_strtoupper($value, 'UTF-8'); |
||||
132 | } |
||||
133 | |||||
134 | /** |
||||
135 | * 获取字符串的长度 |
||||
136 | * |
||||
137 | * @param string $value |
||||
138 | * @return int |
||||
139 | */ |
||||
140 | public static function length(string $value): int |
||||
141 | { |
||||
142 | return mb_strlen($value); |
||||
143 | } |
||||
144 | |||||
145 | /** |
||||
146 | * 截取字符串 |
||||
147 | * |
||||
148 | * @param string $string |
||||
149 | * @param int $start |
||||
150 | * @param int|null $length |
||||
151 | * @return string |
||||
152 | */ |
||||
153 | public static function substr(string $string, int $start, int $length = null): string |
||||
154 | { |
||||
155 | return mb_substr($string, $start, $length, 'UTF-8'); |
||||
156 | } |
||||
157 | |||||
158 | /** |
||||
159 | * 驼峰转下划线 |
||||
160 | * |
||||
161 | * @param string $value |
||||
162 | * @param string $delimiter |
||||
163 | * @return string |
||||
164 | */ |
||||
165 | public static function snake(string $value, string $delimiter = '_'): string |
||||
166 | { |
||||
167 | $key = $value; |
||||
168 | |||||
169 | if (isset(static::$snakeCache[$key][$delimiter])) { |
||||
170 | return static::$snakeCache[$key][$delimiter]; |
||||
171 | } |
||||
172 | |||||
173 | if (!ctype_lower($value)) { |
||||
174 | $value = preg_replace('/\s+/u', '', ucwords($value)); |
||||
175 | |||||
176 | $value = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $value)); |
||||
177 | } |
||||
178 | |||||
179 | return static::$snakeCache[$key][$delimiter] = $value; |
||||
180 | } |
||||
181 | |||||
182 | /** |
||||
183 | * 下划线转驼峰(首字母小写) |
||||
184 | * |
||||
185 | * @param string $value |
||||
186 | * @return string |
||||
187 | */ |
||||
188 | public static function camel(string $value): string |
||||
189 | { |
||||
190 | if (isset(static::$camelCache[$value])) { |
||||
191 | return static::$camelCache[$value]; |
||||
192 | } |
||||
193 | |||||
194 | return static::$camelCache[$value] = lcfirst(static::studly($value)); |
||||
195 | } |
||||
196 | |||||
197 | /** |
||||
198 | * 下划线转驼峰(首字母大写) |
||||
199 | * |
||||
200 | * @param string $value |
||||
201 | * @return string |
||||
202 | */ |
||||
203 | public static function studly(string $value): string |
||||
204 | { |
||||
205 | $key = $value; |
||||
206 | |||||
207 | if (isset(static::$studlyCache[$key])) { |
||||
208 | return static::$studlyCache[$key]; |
||||
209 | } |
||||
210 | |||||
211 | $value = ucwords(str_replace(['-', '_'], ' ', $value)); |
||||
212 | |||||
213 | return static::$studlyCache[$key] = str_replace(' ', '', $value); |
||||
214 | } |
||||
215 | |||||
216 | /** |
||||
217 | * 转为首字母大写的标题格式 |
||||
218 | * |
||||
219 | * @param string $value |
||||
220 | * @return string |
||||
221 | */ |
||||
222 | public static function title(string $value): string |
||||
223 | { |
||||
224 | return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8'); |
||||
225 | } |
||||
226 | } |