字母到按键音数字输出

发布于 2024-10-20 04:05:48 字数 1012 浏览 2 评论 0原文

我尝试用谷歌搜索这个,但不确定什么是最好的寻找。我想做的是翻译文本输入以输出按键式电话的字母。例如 Hello World 会输出 43550 96153 这个想法是我正在尝试使用 tropo 语音 api 系统并希望用户能够输入他们的名字作为按键音值并将其与我的数据库中的数字名称相匹配。

我假设这可以通过一个函数来完成,

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

我确信这会起作用。但是,如果有一个类或一个比将每个字母分解为数字数组更简单的函数,我认为这将是更好的方法。在这一点上,这是一个相当开放式的问题,因为我不知道从哪里开始作为实现这一目标的最佳方法。

编辑:我找到了一个解决方案,不确定它是最好的。

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

input:hello world 
output: 43556 96753

现在我只需要找到一种删除空格的方法:) http://codepad.org/Ieug0Zuw

来源:将数字编码为字母

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

┊风居住的梦幻卍 2024-10-27 04:05:48

PHP 提供了一个名为 strtr 的函数,它可以进行字符串翻译。第一个参数是要翻译的内容,第二个参数是原始字符,第三个参数是替换字符。下面是一个应该可以完成您想要的功能的函数。 编辑:更新了我的示例以删除任何不受支持的字符(除了 az 或空格之外的任何字符)

<?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');

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');
瘫痪情歌 2024-10-27 04:05:48

像这样的结构怎么样...注意:这将忽略无效的“字母”,如空格、标点符号等。

现场演示 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);
    }
?>

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);
    }
?>
撩起发的微风 2024-10-27 04:05:48

我不确定我是否完全理解你在问什么:

  1. 你的输入是单词,你想输出相应的数字?
  2. 你的输入是数字,你想输出对应的单词?

对于情况(1),很简单,只需使用一个数组将字母表中的每个字母映射到相应的数字(即键是字母,值是数字)。然后,您可以迭代输入的字符,并使用数组中相应的元素进行输出。

情况(2)有点棘手。您可以从名称列表中构建一个Trie,并用它来进行查找。

I'm not sure I understand exactly what you are asking:

  1. Your input is words, and you want to output the corresponding numbers?
  2. Your input is numbers, and you want to output the corresponding words?

In case (1), it's simple, just use an array that maps each letter of the alphabet to the corresponding numbers (i.e. key is letter, value is number). You can then just iterate over the characters of the input, and output using the corresponding element in the array.

Case (2) is a bit trickier. You can build a Trie from your list of names, and use it to do the lookup.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文