13
13
use Toolkit \Stdlib \Str \Traits \StringCaseHelperTrait ;
14
14
use Toolkit \Stdlib \Str \Traits \StringCheckHelperTrait ;
15
15
use Toolkit \Stdlib \Str \Traits \StringLengthHelperTrait ;
16
+ use Toolkit \Stdlib \Str \Traits \StringSplitHelperTrait ;
16
17
use Toolkit \Stdlib \Util \UUID ;
17
- use function array_map ;
18
18
use function array_merge ;
19
19
use function array_slice ;
20
- use function array_values ;
21
20
use function base64_encode ;
22
21
use function count ;
23
- use function explode ;
24
22
use function func_get_arg ;
25
23
use function func_num_args ;
26
24
use function function_exists ;
30
28
use function in_array ;
31
29
use function is_int ;
32
30
use function is_string ;
33
- use function mb_convert_encoding ;
34
- use function mb_convert_variables ;
35
- use function mb_detect_encoding ;
36
31
use function mb_internal_encoding ;
37
32
use function mb_strlen ;
38
33
use function mb_strwidth ;
39
34
use function mb_substr ;
40
35
use function preg_match ;
41
36
use function preg_match_all ;
42
- use function preg_split ;
43
37
use function random_bytes ;
44
38
use function str_pad ;
45
39
use function str_repeat ;
46
40
use function str_replace ;
47
- use function str_split ;
48
41
use function strip_tags ;
49
42
use function strlen ;
50
43
use function strpos ;
51
44
use function substr ;
45
+ use function trim ;
52
46
use function uniqid ;
53
47
use function utf8_decode ;
54
48
use function utf8_encode ;
55
- use const PREG_SPLIT_NO_EMPTY ;
56
49
use const STR_PAD_LEFT ;
57
50
use const STR_PAD_RIGHT ;
58
51
59
52
/**
60
53
* Class StringHelper
54
+ *
61
55
* @package Toolkit\Stdlib\Str
62
56
*/
63
57
abstract class StringHelper
@@ -67,6 +61,7 @@ abstract class StringHelper
67
61
use StringCaseHelperTrait;
68
62
use StringCheckHelperTrait;
69
63
use StringLengthHelperTrait;
64
+ use StringSplitHelperTrait;
70
65
71
66
/**
72
67
* @param string $str
@@ -113,7 +108,7 @@ public static function padRight($str, $padLen, string $padStr = ' '): string
113
108
*
114
109
* @return string
115
110
*/
116
- public static function padByWidth ($ str ,$ padLen , string $ padStr = ' ' , int $ padType = STR_PAD_RIGHT ): string
111
+ public static function padByWidth ($ str , $ padLen , string $ padStr = ' ' , int $ padType = STR_PAD_RIGHT ): string
117
112
{
118
113
$ stringWidth = mb_strwidth ((string )$ str , self ::$ defaultEncoding );
119
114
if ($ stringWidth >= $ padLen ) {
@@ -132,11 +127,13 @@ public static function padByWidth($str,$padLen, string $padStr = ' ', int $padTy
132
127
133
128
/**
134
129
* ********************** 生成一定长度的随机字符串函数 **********************
135
- * @param int $length - 随机字符串长度
136
- * @param array|string $param -
137
- * @internal param string $chars
130
+ *
131
+ * @param int $length - 随机字符串长度
132
+ * @param array|string $param -
133
+ *
138
134
* @return string
139
135
* @throws Exception
136
+ * @internal param string $chars
140
137
*/
141
138
public static function random (int $ length , array $ param = []): string
142
139
{
@@ -174,6 +171,7 @@ public static function genSalt(int $length = 32): string
174
171
175
172
/**
176
173
* @param int $length
174
+ *
177
175
* @return bool|string
178
176
*/
179
177
public static function genUid (int $ length = 7 ): string
@@ -187,123 +185,18 @@ public static function genUid(int $length = 7): string
187
185
188
186
/**
189
187
* gen UUID
188
+ *
190
189
* @param int $version
191
190
* @param null $node
192
191
* @param null $ns
192
+ *
193
193
* @return UUID
194
194
*/
195
195
public static function genUUID ($ version = 1 , $ node = null , $ ns = null )
196
196
{
197
197
return UUID ::generate ($ version , $ node , $ ns );
198
198
}
199
199
200
- ////////////////////////////////////////////////////////////////////////
201
- /// Convert to array
202
- ////////////////////////////////////////////////////////////////////////
203
-
204
- /**
205
- * var_dump(str2array('34,56,678, 678, 89, '));
206
- * @param string $str
207
- * @param string $sep
208
- * @return array
209
- */
210
- public static function str2array (string $ str , string $ sep = ', ' ): array
211
- {
212
- $ str = \trim ($ str , "$ sep " );
213
-
214
- if (!$ str ) {
215
- return [];
216
- }
217
-
218
- return preg_split ("/\s* $ sep\s*/ " , $ str , -1 , PREG_SPLIT_NO_EMPTY );
219
- }
220
-
221
- public static function toArray (string $ string , string $ delimiter = ', ' , int $ limit = 0 ): array
222
- {
223
- $ string = \trim ($ string , "$ delimiter " );
224
- if ($ string === '' ) {
225
- return [];
226
- }
227
-
228
- $ values = [];
229
- $ rawList = $ limit < 1 ? explode ($ delimiter , $ string ) : explode ($ delimiter , $ string , $ limit );
230
-
231
- foreach ($ rawList as $ val ) {
232
- if (($ val = \trim ($ val )) !== '' ) {
233
- $ values [] = $ val ;
234
- }
235
- }
236
-
237
- return $ values ;
238
- }
239
-
240
- public static function explode (string $ str , string $ separator = '. ' , int $ limit = 0 ): array
241
- {
242
- return static ::split2Array ($ str , $ separator , $ limit );
243
- }
244
-
245
- /**
246
- * @param string $string
247
- * @param string $delimiter
248
- * @param int $limit
249
- * @return array
250
- */
251
- public static function split2Array (string $ string , string $ delimiter = ', ' , int $ limit = 0 ): array
252
- {
253
- $ string = \trim ($ string , "$ delimiter " );
254
-
255
- if (!strpos ($ string , $ delimiter )) {
256
- return [$ string ];
257
- }
258
-
259
- if ($ limit < 1 ) {
260
- $ list = explode ($ delimiter , $ string );
261
- } else {
262
- $ list = explode ($ delimiter , $ string , $ limit );
263
- }
264
-
265
- return array_values (array_filter (array_map ('trim ' , $ list ), 'strlen ' ));
266
- }
267
-
268
- /**
269
- * @param string $string
270
- * @param int $width
271
- * @return array
272
- */
273
- public static function splitByWidth (string $ string , int $ width ): array
274
- {
275
- // str_split is not suitable for multi-byte characters, we should use preg_split to get char array properly.
276
- // additionally, array_slice() is not enough as some character has doubled width.
277
- // we need a function to split string not by character count but by string width
278
- if (false === $ encoding = mb_detect_encoding ($ string , null , true )) {
279
- return str_split ($ string , $ width );
280
- }
281
-
282
- $ utf8String = mb_convert_encoding ($ string , 'utf8 ' , $ encoding );
283
- $ lines = [];
284
- $ line = '' ;
285
-
286
- foreach (preg_split ('//u ' , $ utf8String ) as $ char ) {
287
- // test if $char could be appended to current line
288
- if (mb_strwidth ($ line . $ char , 'utf8 ' ) <= $ width ) {
289
- $ line .= $ char ;
290
- continue ;
291
- }
292
-
293
- // if not, push current line to array and make new line
294
- $ lines [] = str_pad ($ line , $ width );
295
- $ line = $ char ;
296
- }
297
-
298
- if ('' !== $ line ) {
299
- $ lines [] = count ($ lines ) ? str_pad ($ line , $ width ) : $ line ;
300
- }
301
-
302
- mb_convert_variables ($ encoding , 'utf8 ' , $ lines );
303
-
304
- return $ lines ;
305
- }
306
-
307
200
////////////////////////////////////////////////////////////////////////
308
201
/// Truncate
309
202
////////////////////////////////////////////////////////////////////////
@@ -313,6 +206,7 @@ public static function splitByWidth(string $string, int $width): array
313
206
* @param int $start
314
207
* @param int|null $length
315
208
* @param string $encoding
209
+ *
316
210
* @return bool|string
317
211
*/
318
212
public static function substr (string $ str , int $ start , int $ length = null , string $ encoding = 'utf-8 ' )
@@ -327,9 +221,11 @@ public static function substr(string $str, int $start, int $length = null, strin
327
221
/**
328
222
* @from web
329
223
* utf-8编码下截取中文字符串,参数可以参照substr函数
330
- * @param string $str 要进行截取的字符串
224
+ *
225
+ * @param string $str 要进行截取的字符串
331
226
* @param int $start 要进行截取的开始位置,负数为反向截取
332
- * @param int $end 要进行截取的长度
227
+ * @param int $end 要进行截取的长度
228
+ *
333
229
* @return string
334
230
*/
335
231
public static function utf8SubStr (string $ str , int $ start = 0 , int $ end = null ): string
@@ -364,11 +260,13 @@ public static function utf8SubStr(string $str, int $start = 0, int $end = null):
364
260
/**
365
261
* @from web
366
262
* 中文截取,支持gb2312,gbk,utf-8,big5 *
367
- * @param string $str 要截取的字串
368
- * @param int $start 截取起始位置
369
- * @param int $length 截取长度
263
+ *
264
+ * @param string $str 要截取的字串
265
+ * @param int $start 截取起始位置
266
+ * @param int $length 截取长度
370
267
* @param string $charset utf-8|gb2312|gbk|big5 编码
371
- * @param bool $suffix 是否加尾缀
268
+ * @param bool $suffix 是否加尾缀
269
+ *
372
270
* @return string
373
271
*/
374
272
public static function zhSubStr ($ str , $ start = 0 , $ length = 0 , $ charset = 'utf-8 ' , $ suffix = true ): string
@@ -398,9 +296,11 @@ public static function zhSubStr($str, $start = 0, $length = 0, $charset = 'utf-8
398
296
399
297
/**
400
298
* Truncate strings
299
+ *
401
300
* @param string $str
402
301
* @param int $maxLength Max length
403
- * @param string $suffix Suffix optional
302
+ * @param string $suffix Suffix optional
303
+ *
404
304
* @return string $str truncated
405
305
*/
406
306
/* CAUTION : Use it only on module hookEvents.
@@ -418,9 +318,11 @@ public static function truncate(string $str, $maxLength, $suffix = '...'): strin
418
318
419
319
/**
420
320
* 字符截断输出
321
+ *
421
322
* @param string $str
422
323
* @param int $start
423
324
* @param null|int $length
325
+ *
424
326
* @return string
425
327
*/
426
328
public static function truncate2 (string $ str , int $ start , int $ length = null ): string
@@ -445,9 +347,11 @@ public static function truncate2(string $str, int $start, int $length = null): s
445
347
446
348
/**
447
349
* Copied from CakePHP String utility file
350
+ *
448
351
* @param string $text
449
352
* @param int $length
450
353
* @param array $options
354
+ *
451
355
* @return bool|string
452
356
*/
453
357
public static function truncate3 (string $ text , int $ length = 120 , array $ options = [])
@@ -505,7 +409,8 @@ public static function truncate3(string $text, int $length = 120, array $options
505
409
$ tag [3 ],
506
410
$ entities ,
507
411
PREG_OFFSET_CAPTURE
508
- )) {
412
+ )
413
+ ) {
509
414
foreach ((array )$ entities [0 ] as $ entity ) {
510
415
if ($ entity [1 ] + 1 - $ entities_length <= $ left ) {
511
416
$ left --;
@@ -589,28 +494,30 @@ public static function truncate3(string $text, int $length = 120, array $options
589
494
590
495
/**
591
496
* [format description]
497
+ *
592
498
* @param $str
593
499
* @param array $replaceParams 用于 str_replace('search','replace',$str )
500
+ * @param array $pregParams 用于 preg_replace('pattern','replace',$str)
501
+ *
502
+ * @return string [type] [description]
594
503
* @example
595
- * $replaceParams = [
504
+ * $pregParams = [
505
+ * 'xx', //'pattern'
506
+ * 'yy', //'replace'
507
+ * ]
508
+ * * $pregParams = [
509
+ * ['xx','xx2'], //'pattern'
510
+ * ['yy','yy2'], //'replace'
511
+ * ]
512
+ * @example
513
+ * $replaceParams = [
596
514
* 'xx', //'search'
597
515
* 'yy', //'replace'
598
- * ]
599
- * $replaceParams = [
516
+ * ]
517
+ * $replaceParams = [
600
518
* ['xx','xx2'], //'search'
601
519
* ['yy','yy2'], //'replace'
602
- * ]
603
- * @param array $pregParams 用于 preg_replace('pattern','replace',$str)
604
- * @example
605
- * $pregParams = [
606
- * 'xx', //'pattern'
607
- * 'yy', //'replace'
608
- * ]
609
- * * $pregParams = [
610
- * ['xx','xx2'], //'pattern'
611
- * ['yy','yy2'], //'replace'
612
- * ]
613
- * @return string [type] [description]
520
+ * ]
614
521
*/
615
522
public static function format ($ str , array $ replaceParams = [], array $ pregParams = []): string
616
523
{
@@ -633,7 +540,9 @@ public static function format($str, array $replaceParams = [], array $pregParams
633
540
634
541
/**
635
542
* 格式化,用空格分隔各个词组
636
- * @param string $keyword 字符串
543
+ *
544
+ * @param string $keyword 字符串
545
+ *
637
546
* @return string 格式化后的字符串
638
547
*/
639
548
public static function wordFormat ($ keyword ): string
@@ -651,8 +560,10 @@ public static function wordFormat($keyword): string
651
560
652
561
/**
653
562
* 缩进格式化内容,去空白/注释
563
+ *
654
564
* @param $fileName
655
565
* @param int $type
566
+ *
656
567
* @return mixed
657
568
*/
658
569
public static function deleteStripSpace ($ fileName , $ type = 0 )
0 commit comments