@@ -104,7 +104,8 @@ public static function right($value, $chars = 1)
104
104
*
105
105
* @param mixed $text the text that you're searching
106
106
* Or can be an array of values
107
- * @param ?string $delimiter the text that marks the point before which you want to extract
107
+ * @param null|array|string $delimiter the text that marks the point before which you want to extract
108
+ * Multiple delimiters can be passed as an array of string values
108
109
* @param mixed $instance The instance of the delimiter after which you want to extract the text.
109
110
* By default, this is the first instance (1).
110
111
* A negative value means start searching from the end of the text string.
@@ -132,7 +133,6 @@ public static function before($text, $delimiter, $instance = 1, $matchMode = 0,
132
133
}
133
134
134
135
$ text = Helpers::extractString ($ text ?? '' );
135
- $ delimiter = Helpers::extractString (Functions::flattenSingleValue ($ delimiter ?? '' ));
136
136
$ instance = (int ) $ instance ;
137
137
$ matchMode = (int ) $ matchMode ;
138
138
$ matchEnd = (int ) $ matchEnd ;
@@ -141,13 +141,14 @@ public static function before($text, $delimiter, $instance = 1, $matchMode = 0,
141
141
if (is_array ($ split ) === false ) {
142
142
return $ split ;
143
143
}
144
- if ($ delimiter === '' ) {
144
+ if (Helpers:: extractString (Functions:: flattenSingleValue ( $ delimiter ?? '' )) === '' ) {
145
145
return ($ instance > 0 ) ? '' : $ text ;
146
146
}
147
147
148
148
// Adjustment for a match as the first element of the split
149
149
$ flags = self ::matchFlags ($ matchMode );
150
- $ adjust = preg_match ('/^ ' . preg_quote ($ delimiter ) . "\$/ {$ flags }" , $ split [0 ]);
150
+ $ delimiter = self ::buildDelimiter ($ delimiter );
151
+ $ adjust = preg_match ('/^ ' . $ delimiter . "\$/ {$ flags }" , $ split [0 ]);
151
152
$ oddReverseAdjustment = count ($ split ) % 2 ;
152
153
153
154
$ split = ($ instance < 0 )
@@ -161,7 +162,8 @@ public static function before($text, $delimiter, $instance = 1, $matchMode = 0,
161
162
* TEXTAFTER.
162
163
*
163
164
* @param mixed $text the text that you're searching
164
- * @param ?string $delimiter the text that marks the point before which you want to extract
165
+ * @param null|array|string $delimiter the text that marks the point before which you want to extract
166
+ * Multiple delimiters can be passed as an array of string values
165
167
* @param mixed $instance The instance of the delimiter after which you want to extract the text.
166
168
* By default, this is the first instance (1).
167
169
* A negative value means start searching from the end of the text string.
@@ -189,7 +191,6 @@ public static function after($text, $delimiter, $instance = 1, $matchMode = 0, $
189
191
}
190
192
191
193
$ text = Helpers::extractString ($ text ?? '' );
192
- $ delimiter = Helpers::extractString (Functions::flattenSingleValue ($ delimiter ?? '' ));
193
194
$ instance = (int ) $ instance ;
194
195
$ matchMode = (int ) $ matchMode ;
195
196
$ matchEnd = (int ) $ matchEnd ;
@@ -198,13 +199,14 @@ public static function after($text, $delimiter, $instance = 1, $matchMode = 0, $
198
199
if (is_array ($ split ) === false ) {
199
200
return $ split ;
200
201
}
201
- if ($ delimiter === '' ) {
202
+ if (Helpers:: extractString (Functions:: flattenSingleValue ( $ delimiter ?? '' )) === '' ) {
202
203
return ($ instance < 0 ) ? '' : $ text ;
203
204
}
204
205
205
206
// Adjustment for a match as the first element of the split
206
207
$ flags = self ::matchFlags ($ matchMode );
207
- $ adjust = preg_match ('/^ ' . preg_quote ($ delimiter ) . "\$/ {$ flags }" , $ split [0 ]);
208
+ $ delimiter = self ::buildDelimiter ($ delimiter );
209
+ $ adjust = preg_match ('/^ ' . $ delimiter . "\$/ {$ flags }" , $ split [0 ]);
208
210
$ oddReverseAdjustment = count ($ split ) % 2 ;
209
211
210
212
$ split = ($ instance < 0 )
@@ -215,21 +217,23 @@ public static function after($text, $delimiter, $instance = 1, $matchMode = 0, $
215
217
}
216
218
217
219
/**
220
+ * @param null|array|string $delimiter
218
221
* @param int $matchMode
219
222
* @param int $matchEnd
220
223
* @param mixed $ifNotFound
221
224
*
222
225
* @return string|string[]
223
226
*/
224
- private static function validateTextBeforeAfter (string $ text , string $ delimiter , int $ instance , $ matchMode , $ matchEnd , $ ifNotFound )
227
+ private static function validateTextBeforeAfter (string $ text , $ delimiter , int $ instance , $ matchMode , $ matchEnd , $ ifNotFound )
225
228
{
226
229
$ flags = self ::matchFlags ($ matchMode );
230
+ $ delimiter = self ::buildDelimiter ($ delimiter );
227
231
228
- if (preg_match ('/ ' . preg_quote ( $ delimiter) . "/ {$ flags }" , $ text ) === 0 && $ matchEnd === 0 ) {
232
+ if (preg_match ('/ ' . $ delimiter . "/ {$ flags }" , $ text ) === 0 && $ matchEnd === 0 ) {
229
233
return $ ifNotFound ;
230
234
}
231
235
232
- $ split = preg_split ('/( ' . preg_quote ( $ delimiter) . ") / {$ flags }" , $ text , 0 , PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
236
+ $ split = preg_split ('/ ' . $ delimiter . "/ {$ flags }" , $ text , 0 , PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
233
237
if ($ split === false ) {
234
238
return ExcelError::NA ();
235
239
}
@@ -247,6 +251,28 @@ private static function validateTextBeforeAfter(string $text, string $delimiter,
247
251
return $ split ;
248
252
}
249
253
254
+ /**
255
+ * @param null|array|string $delimiter the text that marks the point before which you want to extract
256
+ * Multiple delimiters can be passed as an array of string values
257
+ */
258
+ private static function buildDelimiter ($ delimiter ): string
259
+ {
260
+ if (is_array ($ delimiter )) {
261
+ $ delimiter = Functions::flattenArray ($ delimiter );
262
+ $ quotedDelimiters = array_map (
263
+ function ($ delimiter ) {
264
+ return preg_quote ($ delimiter ?? '' );
265
+ },
266
+ $ delimiter
267
+ );
268
+ $ delimiters = implode ('| ' , $ quotedDelimiters );
269
+
270
+ return '( ' . $ delimiters . ') ' ;
271
+ }
272
+
273
+ return '( ' . preg_quote ($ delimiter ?? '' ) . ') ' ;
274
+ }
275
+
250
276
private static function matchFlags (int $ matchMode ): string
251
277
{
252
278
return ($ matchMode === 0 ) ? 'mu ' : 'miu ' ;
0 commit comments