Took @65Fbef05's answer, turned it into a func. Note: this is case sensitive like the original explode function.
public static function explodeByArray(array $delimeters, string $input): array {
if($delimeters===[]){
return [$input];
}
$unidelim = $delimeters[0];
$step = str_replace($delimeters, $unidelim, $input);
return explode($unidelim, $step);
}
And unit tested (PHPUnit) as I'm always a bit suspicious of answers on stackoverflow:
/**
* @dataProvider dataProviderForExplodeByArray
* @param array $delimeters
* @param array $expected
* @param string $subject
* @return void
*/
public function testExplodeByArray(array $delimeters, array $expected, string $subject='The quick brown fox jumps over the lazy dog'): void {
self::assertSame($expected, explodeByArray($delimeters, $subject));
}
public function dataProviderForExplodeByArray(): array{
return [
[[], ['The quick brown fox jumps over the lazy dog']],
[['zzz'], ['The quick brown fox jumps over the lazy dog']],
[['the'], ['The quick brown fox jumps over ', ' lazy dog']],
[['The quick brown fox jumps over the lazy dog'], ['', '']],
[['The'], ['', ' quick brown fox jumps over the lazy dog']],
[['dog'], ['The quick brown fox jumps over the lazy ', '']],
[['the', 'quick'], ['The ', ' brown fox jumps over ', ' lazy dog']],
[['quick', 'the'], ['The ', ' brown fox jumps over ', ' lazy dog']],
[['quick', 'the', 'fox'], ['The ', ' brown ', ' jumps over ', ' lazy dog']],
[['over', 'fox'], ['The ', 'y brown ', ' jumps ', ' the lazy ', ''], 'The foxy brown fox jumps over the lazy fox'],
];
}
print_r(preg_split("/[,. ]/", "0 1,2.3"));
will give youArray ( [0] => 0 [1] => 1 [2] => 2 [3] => 3)
. – Lambskin