- array_slice: documentation ( source)
- print_r: documentation ( source)
- preg_split: documentation ( source)
<?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));