Hello all
I have been trying to do this for days and it is driving me round the bend. I am sure it is possible, but I cannot for the life of me figure out how to do it.
I need to interpret touch-tone key presses, and figure out what possible combinations relate to the keys pressed. So it a user entered '222' on their touch tone keypad, this could mean 'aaa', 'aab', 'aac', 'aba', 'abb' ... 'ccc'.
What I want to do is take a variable length input string, and permute it into all possible interpretations, into an indexed array. The element order is not significant, but I figure any way of doing it will probably put elements in the order as specified above.
I would like ALL possible interpretations - I am not bothered about only getting valid character strings from a dictionary.
I am aware this could take a lot of processing for more than a few characters, as the number of possible permuations increases exponentially with every additional key, but this is also not a problem.
I have got this far...
<?php
$query = '222';
$keys = array(
'2'=>array('a','b','c'),
'3'=>array('d','e','f'),
'4'=>array('g','h','i'),
'5'=>array('j','k','l'),
'6'=>array('m','n','o'),
'7'=>array('p','q','r','s'),
'8'=>array('t','u','v'),
'9'=>array('w','x','y','z')
);
$results = array();
$qlength = strlen($query);
for ($i = ($qlength - 1); $i >= 0; $i--) { // Loop through input chars backwards
for ($j = ($qlength - 1); $i >= $j; $j--) { // Loop though chars ahead of current char backwards
// Something horrible which loops through chars an unknown number of times and adds strings to the $results array
}
}
?>
I know this is throughly unhelpful, but all my attempts at building the looping structure have been miserable failures.
The real problem, so far as I can work out, is that the input string is of a variable length (it could be '2', it could be '22', it could be '222' etc etc).
Any ideas gratefully recieved.
Cheers
Chris