10
10
namespace Toolkit \Stdlib \Str \Traits ;
11
11
12
12
use Toolkit \Stdlib \Helper \DataHelper ;
13
+ use function array_filter ;
13
14
use function array_map ;
14
15
use function array_values ;
15
16
use function count ;
@@ -85,43 +86,24 @@ public static function str2ints(string $str, string $delimiter = ',', int $limit
85
86
}
86
87
87
88
/**
88
- * like explode, split string to no empty array
89
- *
90
- * - Difference toNoEmptyArray() and splitTrimFiltered().
91
- *
92
- * Please see {@see StringHelperTest::testDiff_splitTrimFiltered_toNoEmptyArray()}
93
- * So, recommend use toNoEmptyArray() instead splitTrimFiltered()
89
+ * Like explode, but will trim each item and filter empty item.
90
+ * - alias of toNoEmptyArray()
94
91
*
95
92
* @param string $str
96
- * @param string $sep
97
- * @param int $limit
98
- *
99
- * @return array
100
- */
101
- public static function toNoEmptyArray (string $ str , string $ sep = ', ' , int $ limit = -1 ): array
102
- {
103
- $ str = trim ($ str , "$ sep " );
104
- if (!$ str ) {
105
- return [];
106
- }
107
-
108
- $ pattern = $ sep === ' ' ? '/\s+/ ' : "/\s* $ sep\s*/ " ;
109
- return preg_split ($ pattern , $ str , $ limit , PREG_SPLIT_NO_EMPTY );
110
- }
111
-
112
- /**
113
- * @param string $str
114
- * @param string $sep
93
+ * @param string $separator
115
94
* @param int $limit
116
95
*
117
96
* @return array
118
97
*/
119
- public static function splitNoEmptyArray (string $ str , string $ sep = ', ' , int $ limit = 0 ): array
98
+ public static function explode (string $ str , string $ separator = '. ' , int $ limit = 0 ): array
120
99
{
121
- return self ::toNoEmptyArray ($ str , $ sep , $ limit );;
100
+ return self ::toNoEmptyArray ($ str , $ separator , $ limit );
101
+ // return self::splitTrimFiltered($str, $separator, $limit);
122
102
}
123
103
124
104
/**
105
+ * alias of toNoEmptyArray()
106
+ *
125
107
* @param string $str
126
108
* @param string $sep
127
109
* @param int $limit
@@ -134,7 +116,7 @@ public static function toArray(string $str, string $sep = ',', int $limit = 0):
134
116
}
135
117
136
118
/**
137
- * split to array.
119
+ * alias of toNoEmptyArray()
138
120
*
139
121
* @param string $str
140
122
* @param string $sep
@@ -148,56 +130,54 @@ public static function str2array(string $str, string $sep = ',', int $limit = 0)
148
130
}
149
131
150
132
/**
151
- * Like explode, but will trim each item and filter empty item.
133
+ * alias of toNoEmptyArray()
152
134
*
153
135
* @param string $str
154
- * @param string $separator
136
+ * @param string $delimiter
155
137
* @param int $limit
156
138
*
157
139
* @return array
158
140
*/
159
- public static function explode (string $ str , string $ separator = '. ' , int $ limit = 0 ): array
141
+ public static function split2Array (string $ str , string $ delimiter = ', ' , int $ limit = 0 ): array
160
142
{
161
- return self ::toNoEmptyArray ($ str , $ separator , $ limit );
162
- // return self::splitTrimFiltered($str, $separator, $limit);
143
+ return self ::toNoEmptyArray ($ str , $ delimiter , $ limit );
163
144
}
164
145
165
146
/**
166
- * Like explode, but will trim each item and filter empty item.
147
+ * like explode, split string to no empty array
148
+ *
149
+ * - Difference toNoEmptyArray() and splitTrimFiltered().
150
+ *
151
+ * Please see {@see StringHelperTest::testDiff_splitTrimFiltered_toNoEmptyArray()}
152
+ * So, recommend use toNoEmptyArray() instead splitTrimFiltered()
167
153
*
168
154
* @param string $str
169
- * @param string $delimiter
170
- * @param int $limit
155
+ * @param string $sep
156
+ * @param int $limit
171
157
*
172
158
* @return array
173
159
*/
174
- public static function split2Array (string $ str , string $ delimiter = ', ' , int $ limit = 0 ): array
160
+ public static function toNoEmptyArray (string $ str , string $ sep = ', ' , int $ limit = - 1 ): array
175
161
{
176
- return self ::toNoEmptyArray ($ str , $ delimiter , $ limit );
162
+ $ str = trim ($ str , "$ sep " );
163
+ if (!$ str ) {
164
+ return [];
165
+ }
166
+
167
+ $ pattern = $ sep === ' ' ? '/\s+/ ' : "/\s* $ sep\s*/ " ;
168
+ return preg_split ($ pattern , $ str , $ limit , PREG_SPLIT_NO_EMPTY );
177
169
}
178
170
179
171
/**
180
- * Like explode, but will trim each item and filter empty item.
181
- *
182
172
* @param string $str
183
- * @param string $delimiter
173
+ * @param string $sep
184
174
* @param int $limit
185
175
*
186
176
* @return array
187
177
*/
188
- public static function splitTrimFiltered (string $ str , string $ delimiter = ', ' , int $ limit = 0 ): array
178
+ public static function splitNoEmptyArray (string $ str , string $ sep = ', ' , int $ limit = 0 ): array
189
179
{
190
- if (!$ str = trim ($ str )) {
191
- return [];
192
- }
193
-
194
- if (!strpos ($ str , $ delimiter )) {
195
- return [$ str ];
196
- }
197
-
198
- $ list = $ limit < 1 ? explode ($ delimiter , $ str ) : explode ($ delimiter , $ str , $ limit );
199
-
200
- return array_values (array_filter (array_map ('trim ' , $ list ), 'strlen ' ));
180
+ return self ::toNoEmptyArray ($ str , $ sep , $ limit );;
201
181
}
202
182
203
183
/**
@@ -279,6 +259,31 @@ public static function splitTypedList(string $str, string $delimiter = ','): arr
279
259
280
260
return $ arr ;
281
261
}
262
+
263
+ /**
264
+ * Like explode, but will trim each item and filter empty item.
265
+ *
266
+ * @param string $str
267
+ * @param string $delimiter
268
+ * @param int $limit
269
+ *
270
+ * @return array
271
+ */
272
+ public static function splitTrimFiltered (string $ str , string $ delimiter = ', ' , int $ limit = 0 ): array
273
+ {
274
+ if (!$ str = trim ($ str )) {
275
+ return [];
276
+ }
277
+
278
+ if (!strpos ($ str , $ delimiter )) {
279
+ return [$ str ];
280
+ }
281
+
282
+ $ list = $ limit < 1 ? explode ($ delimiter , $ str ) : explode ($ delimiter , $ str , $ limit );
283
+
284
+ return array_values (array_filter (array_map ('trim ' , $ list ), 'strlen ' ));
285
+ }
286
+
282
287
/**
283
288
* @param string $string
284
289
* @param int $width
0 commit comments