The search term you should probably be looking at is something like php string permutations
instead of combinations
. A permutation is an arrangement of items where the order of each item matters (so late
and teal
are different strings), while a combination is an arrangement where the order doesn't matter (late
and teal
are considered identical because they use the same letters).
Here's a few links from Google:
Given an algorithm P
that generates all the permutations of a string, an algorithm C
that generates all the combinations of length N
of some string, and a string S
of length M
, here's another algorithm that will generate all the permutations of S
of length 1...M
:
s = "..."
results = []
for (i = 1; i < s.length; ++i) {
// All combinations of S of length i.
combinations = C(s, i)
// All permutations of S of length i.
results.append(P(combinations))
}
// All permutations of S of length 1..[s.length].
return results