Skip to content

Commit 8c4b16a

Browse files
committed
update some new methods
1 parent 90c5202 commit 8c4b16a

File tree

2 files changed

+229
-144
lines changed

2 files changed

+229
-144
lines changed

src/Str/StringHelper.php

Lines changed: 55 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
1313
use Toolkit\Stdlib\Str\Traits\StringCaseHelperTrait;
1414
use Toolkit\Stdlib\Str\Traits\StringCheckHelperTrait;
1515
use Toolkit\Stdlib\Str\Traits\StringLengthHelperTrait;
16+
use Toolkit\Stdlib\Str\Traits\StringSplitHelperTrait;
1617
use Toolkit\Stdlib\Util\UUID;
17-
use function array_map;
1818
use function array_merge;
1919
use function array_slice;
20-
use function array_values;
2120
use function base64_encode;
2221
use function count;
23-
use function explode;
2422
use function func_get_arg;
2523
use function func_num_args;
2624
use function function_exists;
@@ -30,34 +28,30 @@
3028
use function in_array;
3129
use function is_int;
3230
use function is_string;
33-
use function mb_convert_encoding;
34-
use function mb_convert_variables;
35-
use function mb_detect_encoding;
3631
use function mb_internal_encoding;
3732
use function mb_strlen;
3833
use function mb_strwidth;
3934
use function mb_substr;
4035
use function preg_match;
4136
use function preg_match_all;
42-
use function preg_split;
4337
use function random_bytes;
4438
use function str_pad;
4539
use function str_repeat;
4640
use function str_replace;
47-
use function str_split;
4841
use function strip_tags;
4942
use function strlen;
5043
use function strpos;
5144
use function substr;
45+
use function trim;
5246
use function uniqid;
5347
use function utf8_decode;
5448
use function utf8_encode;
55-
use const PREG_SPLIT_NO_EMPTY;
5649
use const STR_PAD_LEFT;
5750
use const STR_PAD_RIGHT;
5851

5952
/**
6053
* Class StringHelper
54+
*
6155
* @package Toolkit\Stdlib\Str
6256
*/
6357
abstract class StringHelper
@@ -67,6 +61,7 @@ abstract class StringHelper
6761
use StringCaseHelperTrait;
6862
use StringCheckHelperTrait;
6963
use StringLengthHelperTrait;
64+
use StringSplitHelperTrait;
7065

7166
/**
7267
* @param string $str
@@ -113,7 +108,7 @@ public static function padRight($str, $padLen, string $padStr = ' '): string
113108
*
114109
* @return string
115110
*/
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
117112
{
118113
$stringWidth = mb_strwidth((string)$str, self::$defaultEncoding);
119114
if ($stringWidth >= $padLen) {
@@ -132,11 +127,13 @@ public static function padByWidth($str,$padLen, string $padStr = ' ', int $padTy
132127

133128
/**
134129
* ********************** 生成一定长度的随机字符串函数 **********************
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+
*
138134
* @return string
139135
* @throws Exception
136+
* @internal param string $chars
140137
*/
141138
public static function random(int $length, array $param = []): string
142139
{
@@ -174,6 +171,7 @@ public static function genSalt(int $length = 32): string
174171

175172
/**
176173
* @param int $length
174+
*
177175
* @return bool|string
178176
*/
179177
public static function genUid(int $length = 7): string
@@ -187,123 +185,18 @@ public static function genUid(int $length = 7): string
187185

188186
/**
189187
* gen UUID
188+
*
190189
* @param int $version
191190
* @param null $node
192191
* @param null $ns
192+
*
193193
* @return UUID
194194
*/
195195
public static function genUUID($version = 1, $node = null, $ns = null)
196196
{
197197
return UUID::generate($version, $node, $ns);
198198
}
199199

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-
307200
////////////////////////////////////////////////////////////////////////
308201
/// Truncate
309202
////////////////////////////////////////////////////////////////////////
@@ -313,6 +206,7 @@ public static function splitByWidth(string $string, int $width): array
313206
* @param int $start
314207
* @param int|null $length
315208
* @param string $encoding
209+
*
316210
* @return bool|string
317211
*/
318212
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
327221
/**
328222
* @from web
329223
* utf-8编码下截取中文字符串,参数可以参照substr函数
330-
* @param string $str 要进行截取的字符串
224+
*
225+
* @param string $str 要进行截取的字符串
331226
* @param int $start 要进行截取的开始位置,负数为反向截取
332-
* @param int $end 要进行截取的长度
227+
* @param int $end 要进行截取的长度
228+
*
333229
* @return string
334230
*/
335231
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):
364260
/**
365261
* @from web
366262
* 中文截取,支持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 截取长度
370267
* @param string $charset utf-8|gb2312|gbk|big5 编码
371-
* @param bool $suffix 是否加尾缀
268+
* @param bool $suffix 是否加尾缀
269+
*
372270
* @return string
373271
*/
374272
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
398296

399297
/**
400298
* Truncate strings
299+
*
401300
* @param string $str
402301
* @param int $maxLength Max length
403-
* @param string $suffix Suffix optional
302+
* @param string $suffix Suffix optional
303+
*
404304
* @return string $str truncated
405305
*/
406306
/* CAUTION : Use it only on module hookEvents.
@@ -418,9 +318,11 @@ public static function truncate(string $str, $maxLength, $suffix = '...'): strin
418318

419319
/**
420320
* 字符截断输出
321+
*
421322
* @param string $str
422323
* @param int $start
423324
* @param null|int $length
325+
*
424326
* @return string
425327
*/
426328
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
445347

446348
/**
447349
* Copied from CakePHP String utility file
350+
*
448351
* @param string $text
449352
* @param int $length
450353
* @param array $options
354+
*
451355
* @return bool|string
452356
*/
453357
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
505409
$tag[3],
506410
$entities,
507411
PREG_OFFSET_CAPTURE
508-
)) {
412+
)
413+
) {
509414
foreach ((array)$entities[0] as $entity) {
510415
if ($entity[1] + 1 - $entities_length <= $left) {
511416
$left--;
@@ -589,28 +494,30 @@ public static function truncate3(string $text, int $length = 120, array $options
589494

590495
/**
591496
* [format description]
497+
*
592498
* @param $str
593499
* @param array $replaceParams 用于 str_replace('search','replace',$str )
500+
* @param array $pregParams 用于 preg_replace('pattern','replace',$str)
501+
*
502+
* @return string [type] [description]
594503
* @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 = [
596514
* 'xx', //'search'
597515
* 'yy', //'replace'
598-
* ]
599-
* $replaceParams = [
516+
* ]
517+
* $replaceParams = [
600518
* ['xx','xx2'], //'search'
601519
* ['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+
* ]
614521
*/
615522
public static function format($str, array $replaceParams = [], array $pregParams = []): string
616523
{
@@ -633,7 +540,9 @@ public static function format($str, array $replaceParams = [], array $pregParams
633540

634541
/**
635542
* 格式化,用空格分隔各个词组
636-
* @param string $keyword 字符串
543+
*
544+
* @param string $keyword 字符串
545+
*
637546
* @return string 格式化后的字符串
638547
*/
639548
public static function wordFormat($keyword): string
@@ -651,8 +560,10 @@ public static function wordFormat($keyword): string
651560

652561
/**
653562
* 缩进格式化内容,去空白/注释
563+
*
654564
* @param $fileName
655565
* @param int $type
566+
*
656567
* @return mixed
657568
*/
658569
public static function deleteStripSpace($fileName, $type = 0)

0 commit comments

Comments
 (0)