3v4l.org

run code in 300+ PHP versions simultaneously
<?php function computeAllPermutations(array $words, array $separators): array { switch (count($words)) { case 0: return []; case 1: return [$words[0]]; default: $permutations = []; foreach (computeAllPermutations(array_slice($words, 1), $separators) as $subPermutation) { foreach ($separators as $separator) { $permutations[] = $words[0] . $separator . $subPermutation; } } return $permutations; } } $char = ["-", "_", "|", "+", "~"]; $MyString = "this is test string"; $words = preg_split('/\h+/', $MyString); print_r(computeAllPermutations($words, $char));

preferences:
24.41 ms | 406 KiB | 5 Q