phpclassfunctiontropo

letters to touch tone number output


I tried Googling this but not sure what's the best thing to look for. What I am trying to do is to translate a text input to output the letters of a touch tone phone. For example Hello World would output 43550 96153the idea is I'm trying to use the tropo voice api system and want the user to be able to enter their name as touch tone values and match that to their name as numbers in my database.

I'm assuming this can be done with a function along the lines of

$input= $touchtone_value;  
$number_two_array (a,b,c);  
if( $input==in_array($number_two_array)){ 
        $output = '2';
}

I'm sure this will work. However, if there is a class out there or a simpler function than to break each letter into number arrays I think that would be a better way to do it. At this point this is a fairly open ended question as I have NO IDEA where to start as the best way to accomplish this.

EDIT: I found a solution, not sure it's the best one.

            $input = strtolower('HELLO WORLD');
            echo  'input: '. $input. "\n";
            echo $output = 'output: '.  strtr($input,'abcdefghijklmnopqrstuvwxyz', '22233344455566677778889999'); 

input:hello world 
output: 43556 96753

Now I just need to find a way to remove white space :) http://codepad.org/Ieug0Zuw

Source: code a number into letters


Solution

  • How about a structure like this... NOTE: This will ignore invalid 'letters' like spaces, punctuation, etc..

    LIVE DEMO http://codepad.org/pQHGhm7Y

    <?php
        echo getNumbersFromText('Hello There').'<br />';
        echo getNumbersFromText('This is a really long text string').'<br />';
    
        function getNumbersFromText($inp){
            $result=array();
            $inp = strtolower($inp);
            $keypad = array('a' => '2', 'b' => '2', 'c' => '2', 'd' => '3',
                'e' => '3', 'f' => '3', 'g' => '4', 'h' => '4',
                'i' => '4', 'j' => '5', 'k' => '5', 'l' => '5',
                'm' => '6', 'n' => '6', 'o' => '6', 'p' => '7',
                'q' => '7', 'r' => '7', 's' => '7', 't' => '8',
                'u' => '8', 'v' => '8', 'w' => '9', 'x' => '9',
                'y' => '9', 'z' => '9');
    
            for ($x=0; $x<strlen($inp); $x++){
                $letter = $inp[$x];
                if ($keypad[$letter]) $result[]= $keypad[$letter];
            }
            return implode('',$result);
        }
    ?>