@@ -104,7 +104,8 @@ public static function right($value, $chars = 1)
104104 *
105105 * @param mixed $text the text that you're searching
106106 * 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
108109 * @param mixed $instance The instance of the delimiter after which you want to extract the text.
109110 * By default, this is the first instance (1).
110111 * 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,
132133 }
133134
134135 $ text = Helpers::extractString ($ text ?? '' );
135- $ delimiter = Helpers::extractString (Functions::flattenSingleValue ($ delimiter ?? '' ));
136136 $ instance = (int ) $ instance ;
137137 $ matchMode = (int ) $ matchMode ;
138138 $ matchEnd = (int ) $ matchEnd ;
@@ -141,13 +141,14 @@ public static function before($text, $delimiter, $instance = 1, $matchMode = 0,
141141 if (is_array ($ split ) === false ) {
142142 return $ split ;
143143 }
144- if ($ delimiter === '' ) {
144+ if (Helpers:: extractString (Functions:: flattenSingleValue ( $ delimiter ?? '' )) === '' ) {
145145 return ($ instance > 0 ) ? '' : $ text ;
146146 }
147147
148148 // Adjustment for a match as the first element of the split
149149 $ 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 ]);
151152 $ oddReverseAdjustment = count ($ split ) % 2 ;
152153
153154 $ split = ($ instance < 0 )
@@ -161,7 +162,8 @@ public static function before($text, $delimiter, $instance = 1, $matchMode = 0,
161162 * TEXTAFTER.
162163 *
163164 * @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
165167 * @param mixed $instance The instance of the delimiter after which you want to extract the text.
166168 * By default, this is the first instance (1).
167169 * 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, $
189191 }
190192
191193 $ text = Helpers::extractString ($ text ?? '' );
192- $ delimiter = Helpers::extractString (Functions::flattenSingleValue ($ delimiter ?? '' ));
193194 $ instance = (int ) $ instance ;
194195 $ matchMode = (int ) $ matchMode ;
195196 $ matchEnd = (int ) $ matchEnd ;
@@ -198,13 +199,14 @@ public static function after($text, $delimiter, $instance = 1, $matchMode = 0, $
198199 if (is_array ($ split ) === false ) {
199200 return $ split ;
200201 }
201- if ($ delimiter === '' ) {
202+ if (Helpers:: extractString (Functions:: flattenSingleValue ( $ delimiter ?? '' )) === '' ) {
202203 return ($ instance < 0 ) ? '' : $ text ;
203204 }
204205
205206 // Adjustment for a match as the first element of the split
206207 $ 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 ]);
208210 $ oddReverseAdjustment = count ($ split ) % 2 ;
209211
210212 $ split = ($ instance < 0 )
@@ -215,21 +217,23 @@ public static function after($text, $delimiter, $instance = 1, $matchMode = 0, $
215217 }
216218
217219 /**
220+ * @param null|array|string $delimiter
218221 * @param int $matchMode
219222 * @param int $matchEnd
220223 * @param mixed $ifNotFound
221224 *
222225 * @return string|string[]
223226 */
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 )
225228 {
226229 $ flags = self ::matchFlags ($ matchMode );
230+ $ delimiter = self ::buildDelimiter ($ delimiter );
227231
228- if (preg_match ('/ ' . preg_quote ( $ delimiter) . "/ {$ flags }" , $ text ) === 0 && $ matchEnd === 0 ) {
232+ if (preg_match ('/ ' . $ delimiter . "/ {$ flags }" , $ text ) === 0 && $ matchEnd === 0 ) {
229233 return $ ifNotFound ;
230234 }
231235
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 );
233237 if ($ split === false ) {
234238 return ExcelError::NA ();
235239 }
@@ -247,6 +251,28 @@ private static function validateTextBeforeAfter(string $text, string $delimiter,
247251 return $ split ;
248252 }
249253
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+
250276 private static function matchFlags (int $ matchMode ): string
251277 {
252278 return ($ matchMode === 0 ) ? 'mu ' : 'miu ' ;
0 commit comments