Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 425
Brooke. Avatar asked Mar 02 '11 21:03

Brooke.


2 Answers

PHP provides a function called strtr which does string translations. The first argument is what you want to translate, the second is the original characters, the third is the replacement characters. Below is a function that should do what you want. Edit: Updated my sample to strip out any characters that aren't supported (anything other than a-z or a space)

<?php

function get_tones($inp) {
    $from = 'abcdefghijklmnopqrstuvwxyz ';
    $to = '222333444555666777788899990';

    // convert the input to lower case
    $inp = strtolower($inp);

    // remove anything that isn't a letter or a space
    $inp = preg_replace('/[^a-z ]/', '', $inp);

    return strtr($inp, $from, $to);
}

assert(get_tones('Hello world') == '43556096753');
assert(get_tones('Hell234"*&o world') == '43556096753');
assert(get_tones('ALEX') == '2539');
assert(get_tones('    ') == '0000');
like image 182
Alex Vidal Avatar answered Oct 17 '22 13:10

Alex Vidal


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);
    }
?>
like image 35
Dutchie432 Avatar answered Oct 17 '22 13:10

Dutchie432