tags:

views:

93

answers:

1

I searched, can't find anything: How do I do this?

I enter $x = 'tlagre';

I want it to return every single letter combination, down to four.

i.e. - "tlagr" "gratl" "lat" "rat"

I've found some that can do this but none will do every number of letters (they all just do the exact same # of letters as given in $x

+4  A: 

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
John Feminella