<?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));
- Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.26, 8.3.0 - 8.3.14, 8.4.1
- Array
(
[0] => this-is-test-string
[1] => this_is-test-string
[2] => this|is-test-string
[3] => this+is-test-string
[4] => this~is-test-string
[5] => this-is_test-string
[6] => this_is_test-string
[7] => this|is_test-string
[8] => this+is_test-string
[9] => this~is_test-string
[10] => this-is|test-string
[11] => this_is|test-string
[12] => this|is|test-string
[13] => this+is|test-string
[14] => this~is|test-string
[15] => this-is+test-string
[16] => this_is+test-string
[17] => this|is+test-string
[18] => this+is+test-string
[19] => this~is+test-string
[20] => this-is~test-string
[21] => this_is~test-string
[22] => this|is~test-string
[23] => this+is~test-string
[24] => this~is~test-string
[25] => this-is-test_string
[26] => this_is-test_string
[27] => this|is-test_string
[28] => this+is-test_string
[29] => this~is-test_string
[30] => this-is_test_string
[31] => this_is_test_string
[32] => this|is_test_string
[33] => this+is_test_string
[34] => this~is_test_string
[35] => this-is|test_string
[36] => this_is|test_string
[37] => this|is|test_string
[38] => this+is|test_string
[39] => this~is|test_string
[40] => this-is+test_string
[41] => this_is+test_string
[42] => this|is+test_string
[43] => this+is+test_string
[44] => this~is+test_string
[45] => this-is~test_string
[46] => this_is~test_string
[47] => this|is~test_string
[48] => this+is~test_string
[49] => this~is~test_string
[50] => this-is-test|string
[51] => this_is-test|string
[52] => this|is-test|string
[53] => this+is-test|string
[54] => this~is-test|string
[55] => this-is_test|string
[56] => this_is_test|string
[57] => this|is_test|string
[58] => this+is_test|string
[59] => this~is_test|string
[60] => this-is|test|string
[61] => this_is|test|string
[62] => this|is|test|string
[63] => this+is|test|string
[64] => this~is|test|string
[65] => this-is+test|string
[66] => this_is+test|string
[67] => this|is+test|string
[68] => this+is+test|string
[69] => this~is+test|string
[70] => this-is~test|string
[71] => this_is~test|string
[72] => this|is~test|string
[73] => this+is~test|string
[74] => this~is~test|string
[75] => this-is-test+string
[76] => this_is-test+string
[77] => this|is-test+string
[78] => this+is-test+string
[79] => this~is-test+string
[80] => this-is_test+string
[81] => this_is_test+string
[82] => this|is_test+string
[83] => this+is_test+string
[84] => this~is_test+string
[85] => this-is|test+string
[86] => this_is|test+string
[87] => this|is|test+string
[88] => this+is|test+string
[89] => this~is|test+string
[90] => this-is+test+string
[91] => this_is+test+string
[92] => this|is+test+string
[93] => this+is+test+string
[94] => this~is+test+string
[95] => this-is~test+string
[96] => this_is~test+string
[97] => this|is~test+string
[98] => this+is~test+string
[99] => this~is~test+string
[100] => this-is-test~string
[101] => this_is-test~string
[102] => this|is-test~string
[103] => this+is-test~string
[104] => this~is-test~string
[105] => this-is_test~string
[106] => this_is_test~string
[107] => this|is_test~string
[108] => this+is_test~string
[109] => this~is_test~string
[110] => this-is|test~string
[111] => this_is|test~string
[112] => this|is|test~string
[113] => this+is|test~string
[114] => this~is|test~string
[115] => this-is+test~string
[116] => this_is+test~string
[117] => this|is+test~string
[118] => this+is+test~string
[119] => this~is+test~string
[120] => this-is~test~string
[121] => this_is~test~string
[122] => this|is~test~string
[123] => this+is~test~string
[124] => this~is~test~string
)
preferences:
104.39 ms | 418 KiB | 5 Q