Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c3a38a2

Browse files
committed
up: update and add some string helper methods
1 parent 397a029 commit c3a38a2

File tree

3 files changed

+80
-11
lines changed

3 files changed

+80
-11
lines changed

‎src/Str/Traits/StringCheckHelperTrait.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,17 @@ public static function hasSuffixes(string $str, array|string $needles): bool
476476
*/
477477
public static function isVarName(string $string): bool
478478
{
479-
return preg_match('@^[a-zA-Z_\x7f-\xff][a-zA-Z\d_\x7f-\xff]*@i', $string) === 1;
479+
return preg_match('@^[a-zA-Z_\x7f-\xff][a-zA-Z\d_\x7f-\xff]*$@i', $string) === 1;
480+
}
481+
482+
/**
483+
* @param string $str
484+
*
485+
* @return bool
486+
*/
487+
public static function isAlphaNum(string $str): bool
488+
{
489+
return preg_match('/^[a-zA-Z0-9]+$/', $str) === 1;
480490
}
481491

482492
/**

‎src/Str/Traits/StringConvertTrait.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use function array_values;
1717
use function count;
1818
use function explode;
19+
use function implode;
1920
use function is_numeric;
2021
use function mb_convert_encoding;
2122
use function mb_convert_variables;
@@ -118,6 +119,24 @@ public static function toTyped(string $str, bool $parseBool = false, int $intMax
118119
return $str;
119120
}
120121

122+
/**
123+
* like implode() but support any type
124+
*
125+
* @param array $list
126+
* @param string $sep
127+
*
128+
* @return string
129+
*/
130+
public static function join(array $list, string $sep = ','): string
131+
{
132+
$strings = [];
133+
foreach ($list as $value) {
134+
$strings[] = DataHelper::toString($value, true);
135+
}
136+
137+
return implode($sep, $strings);
138+
}
139+
121140
////////////////////////////////////////////////////////////////////////
122141
/// split to array
123142
////////////////////////////////////////////////////////////////////////
@@ -452,4 +471,44 @@ public static function splitUnicode2(string $str, int $length = 1): array
452471

453472
return $tmp;
454473
}
474+
475+
/**
476+
* split string to two-node strings.
477+
*
478+
* eg: "name=tom" => ['name', 'tom']
479+
*
480+
* @param string $str
481+
* @param string $sep
482+
*
483+
* @return array
484+
*/
485+
public static function split2node(string $str, string $sep = '='): array
486+
{
487+
$nodes = self::splitTrimmed($str, $sep, 2);
488+
489+
return count($nodes) === 2 ? $nodes : [$nodes[0], ''];
490+
}
491+
492+
/**
493+
* @param string $text multi line text
494+
* @param string $lineSep
495+
* @param string $valSep
496+
*
497+
* @return array
498+
*/
499+
public static function splitKvMap(string $text, string $lineSep = "\n", string $valSep = '='): array
500+
{
501+
$map = [];
502+
503+
foreach (explode($lineSep, $text) as $line) {
504+
if (!$line = trim($line)) {
505+
continue;
506+
}
507+
508+
[$k, $v] = self::split2node($line, $valSep);
509+
$map[$k] = $v;
510+
}
511+
512+
return $map;
513+
}
455514
}

‎src/Str/Traits/StringLengthHelperTrait.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ public static function len(int|string $string): int
4848
}
4949

5050
/**
51-
* @param string $str
51+
* @param string|int $str
5252
* @param string $encoding
5353
*
5454
* @return int
5555
*/
56-
public static function len2(string $str, string $encoding = 'UTF-8'): int
56+
public static function len2(string|int $str, string $encoding = 'UTF-8'): int
5757
{
5858
$str = (string)$str;
5959

@@ -79,11 +79,11 @@ public static function strlen(mixed $str, string $encoding = 'UTF-8', bool $deco
7979
}
8080

8181
/**
82-
* @param string $string
82+
* @param string|int $string $string
8383
*
8484
* @return int
8585
*/
86-
public static function utf8Len(string $string): int
86+
public static function utf8Len(string|int $string): int
8787
{
8888
// strlen: one chinese is 3 char.
8989
// mb_strlen: one chinese is 1 char.
@@ -92,11 +92,11 @@ public static function utf8Len(string $string): int
9292
}
9393

9494
/**
95-
* @param string $string
95+
* @param string|int $string
9696
*
9797
* @return int
9898
*/
99-
public static function utf8width(string $string): int
99+
public static function utf8width(string|int $string): int
100100
{
101101
// strlen: one chinese is 3 char.
102102
// mb_strlen: one chinese is 1 char.
@@ -107,21 +107,21 @@ public static function utf8width(string $string): int
107107
/**
108108
* 计算字符长度
109109
*
110-
* @param string $str
110+
* @param string|int $str
111111
*
112112
* @return int
113113
*/
114-
public static function length(string $str): int
114+
public static function length(string|int $str): int
115115
{
116116
if ($str === '') {
117117
return 0;
118118
}
119119

120120
if (function_exists('mb_strlen')) {
121-
return mb_strlen($str, 'utf-8');
121+
return mb_strlen((string)$str, 'utf-8');
122122
}
123123

124-
preg_match_all('/./u', $str, $arr);
124+
preg_match_all('/./u', (string)$str, $arr);
125125

126126
return count($arr[0]);
127127
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /