I don't know if there's a really smart way of doing this. Of course, you could try all possible words, but that would not be very effective. Quite the opposite in fact. For a 8 letter word, there would be a total of 8! combinations of 8-letter words, which is 40320 combinations (8! = 8*7*6*5*4*3*2*1). Then plus all the combinations when only using 7 letters, 6 letters and so on.
So the total amount of combinations would be
8 1-letter words, plus (8*7) 2-letter words, (8*7*6) 3-letter words etc.
Which equals 109 600 combinations in total
And just to make a point of how ineffective this is, and how fast the count of possible combinations increase.
8! = 40 320
10! = 3 628 800
15! = 1 307 674 368 000
Any way, here is a function which will try all possible combinations. You'll have to figure out how to validate a word against a dictionary your self :P
PHP Code:
function tryAll($current, $letters) {
for($i = 0; $i < count($letters); $i++) {
$letter = $letters[$i];
$str = $current.$letter;
// Check if $str is a valid word, with a dictionary in a
// database, file or what ever smart solution you may find
// Make a copy of $letters, without the currently one selected
// (since that letter is already used, you dont wanna use it again)
$copy = $letters;
unset($copy[$i]);
// run recursive call to same function
tryAll($str, $copy);
}
}
$word = array('e', 't', 'h', 'i', 'o', 'p', 'i', 'a');
tryAll('', $word);
I dont know if this will help understanding it, but what is does is basically add the first available letter and check if it's a valid word, add the next letter and check again etc. When all letters are used, it backs up to the last point where it had several letters left, and try the other one. Then it keeps doing that until it has backed all the way to the start and tried all the different letters.
It would go like this
e, et, eth, ethi, ethio, ethiop, ethiopi, ethiopia, ethiopa, ethiopai, ethioi, ethioip, ethioipa, ethioia, ethioiap, ethioa, ethioap, ethioapi, ethioai, ethioaip ...