3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php // Returns the total number of $count-length strings generatable from $letters. function getPermCount($letters, $count) { $result = 1; // k characters from a set of n has n!/(n-k)! possible combinations for($i = strlen($letters) - $count + 1; $i <= strlen($letters); $i++) { $result *= $i; } return $result; } // Decodes $index to a $count-length string from $letters, no repeat chars. function getPerm($letters, $count, $index) { $result = ''; for($i = 0; $i < $count; $i++) { $pos = $index % strlen($letters); $result .= $letters[$pos]; $index = ($index-$pos)/strlen($letters); $letters = substr($letters, 0, $pos) . substr($letters, $pos+1); } return $result; } $letters = 'x1 x2 x3'; $r = array(); //echo '1 letters from 4: '; for($i = 0; $i < getPermCount($letters, 1); $i++) $r[] = getPerm($letters, 1, $i); //echo '2 letters from 4: '; for($i = 0; $i < getPermCount($letters, 2); $i++) $r[] = getPerm($letters, 2, $i); //echo ' 3 letters from 4: '; for($i = 0; $i < getPermCount($letters, 3); $i++) $r[] = getPerm($letters, 3, $i); print_r($r);
Output for 4.3.0 - 7.1.0
Array ( [0] => x [1] => 1 [2] => [3] => x [4] => 2 [5] => [6] => x [7] => 3 [8] => x1 [9] => 1x [10] => x [11] => xx [12] => 2x [13] => x [14] => xx [15] => 3x [16] => x [17] => 1 [18] => 1 [19] => x1 [20] => 21 [21] => 1 [22] => x1 [23] => 31 [24] => xx [25] => 1x [26] => x [27] => x [28] => 2 [29] => [30] => x [31] => 3 [32] => x2 [33] => 12 [34] => 2 [35] => x2 [36] => 2x [37] => x [38] => xx [39] => 3x [40] => x [41] => 1 [42] => [43] => x [44] => 2 [45] => 2 [46] => x2 [47] => 32 [48] => xx [49] => 1x [50] => x [51] => xx [52] => 2x [53] => x [54] => x [55] => 3 [56] => x3 [57] => 13 [58] => 3 [59] => x3 [60] => 23 [61] => 3 [62] => x3 [63] => 3x [64] => x1 [65] => 1x [66] => x1 [67] => xx1 [68] => 2x1 [69] => x1 [70] => xx1 [71] => 3x1 [72] => x 1 [73] => 1 x [74] => 1x [75] => x1x [76] => 21x [77] => 1x [78] => x1x [79] => 31x [80] => xx1 [81] => 1xx [82] => xx [83] => x x [84] => 2 x [85] => x [86] => x x [87] => 3 x [88] => x21 [89] => 12x [90] => 2x [91] => x2x [92] => 2xx [93] => xx [94] => xxx [95] => 3xx [96] => x 1 [97] => 1 x [98] => x [99] => x x [100] => 2 x [101] => 2x [102] => x2x [103] => 32x [104] => xx1 [105] => 1xx [106] => xx [107] => xxx [108] => 2xx [109] => xx [110] => x x [111] => 3 x [112] => x31 [113] => 13x [114] => 3x [115] => x3x [116] => 23x [117] => 3x [118] => x3x [119] => 3xx [120] => x1x [121] => 1xx [122] => xx [123] => xx [124] => 2x [125] => x [126] => xx [127] => 3x [128] => x x [129] => 1 x [130] => 1x [131] => x1 [132] => 21 [133] => 1 [134] => x1 [135] => 31 [136] => xx [137] => 1x [138] => x1 [139] => x 1 [140] => 2 1 [141] => 1 [142] => x 1 [143] => 3 1 [144] => x2 [145] => 12 [146] => 21 [147] => x21 [148] => 2x1 [149] => x1 [150] => xx1 [151] => 3x1 [152] => x [153] => 1 [154] => 1 [155] => x 1 [156] => 2 1 [157] => 21 [158] => x21 [159] => 321 [160] => xx [161] => 1x [162] => x1 [163] => xx1 [164] => 2x1 [165] => x1 [166] => x 1 [167] => 3 1 [168] => x3 [169] => 13 [170] => 31 [171] => x31 [172] => 231 [173] => 31 [174] => x31 [175] => 3x1 [176] => x12 [177] => 1x2 [178] => x2 [179] => xx2 [180] => 2xx [181] => xx [182] => xxx [183] => 3xx [184] => x 2 [185] => 1 2 [186] => 12 [187] => x12 [188] => 21x [189] => 1x [190] => x1x [191] => 31x [192] => xx2 [193] => 1x2 [194] => x2 [195] => x 2 [196] => 2 x [197] => x [198] => x x [199] => 3 x [200] => x2x [201] => 12x [202] => 2x [203] => x2 [204] => 2x [205] => x [206] => xx [207] => 3x [208] => x x [209] => 1 x [210] => x [211] => x [212] => 2 [213] => 2 [214] => x2 [215] => 32 [216] => xxx [217] => 1xx [218] => xx [219] => xx [220] => 2x [221] => x [222] => x [223] => 3 [224] => x3x [225] => 13x [226] => 3x [227] => x3 [228] => 23 [229] => 3 [230] => x3 [231] => 3x [232] => x1 [233] => 1x [234] => x [235] => xx [236] => 2x [237] => x2 [238] => xx2 [239] => 3x2 [240] => x [241] => 1 [242] => 1 [243] => x1 [244] => 21 [245] => 12 [246] => x12 [247] => 312 [248] => xx [249] => 1x [250] => x [251] => x [252] => 2 [253] => 2 [254] => x 2 [255] => 3 2 [256] => x2 [257] => 12 [258] => 2 [259] => x2 [260] => 2x [261] => x2 [262] => xx2 [263] => 3x2 [264] => x 2 [265] => 1 2 [266] => 2 [267] => x 2 [268] => 2 x [269] => 2x [270] => x2x [271] => 32x [272] => xx2 [273] => 1x2 [274] => x2 [275] => xx2 [276] => 2xx [277] => xx [278] => x x [279] => 3 x [280] => x32 [281] => 132 [282] => 32 [283] => x32 [284] => 23x [285] => 3x [286] => x3x [287] => 3xx [288] => x1x [289] => 1xx [290] => xx [291] => xxx [292] => 2xx [293] => xx [294] => xx [295] => 3x [296] => x x [297] => 1 x [298] => 1x [299] => x1x [300] => 21x [301] => 1x [302] => x1 [303] => 31 [304] => xxx [305] => 1xx [306] => xx [307] => x x [308] => 2 x [309] => x [310] => x [311] => 3 [312] => x2x [313] => 12x [314] => 2x [315] => x2x [316] => 2xx [317] => xx [318] => xx [319] => 3x [320] => x x [321] => 1 x [322] => x [323] => x x [324] => 2 x [325] => 2x [326] => x2 [327] => 32 [328] => xx [329] => 1x [330] => x [331] => xx [332] => 2x [333] => x2 [334] => x 2 [335] => 3 2 [336] => x3 [337] => 13 [338] => 3 [339] => x3 [340] => 23 [341] => 32 [342] => x32 [343] => 3x2 [344] => x13 [345] => 1x3 [346] => x3 [347] => xx3 [348] => 2x3 [349] => x3 [350] => xx3 [351] => 3xx [352] => x 3 [353] => 1 3 [354] => 13 [355] => x13 [356] => 213 [357] => 13 [358] => x13 [359] => 31x [360] => xx3 [361] => 1x3 [362] => x3 [363] => x 3 [364] => 2 3 [365] => 3 [366] => x 3 [367] => 3 x [368] => x23 [369] => 123 [370] => 23 [371] => x23 [372] => 2x3 [373] => x3 [374] => xx3 [375] => 3xx [376] => x 3 [377] => 1 3 [378] => 3 [379] => x 3 [380] => 2 3 [381] => 23 [382] => x23 [383] => 32x [384] => xx3 [385] => 1x3 [386] => x3 [387] => xx3 [388] => 2x3 [389] => x3 [390] => x 3 [391] => 3 x [392] => x3x [393] => 13x [394] => 3x [395] => x3x [396] => 23x [397] => 3x [398] => x3 [399] => 3x )