字母到按键音数字输出
我尝试用谷歌搜索这个,但不确定什么是最好的寻找。我想做的是翻译文本输入以输出按键式电话的字母。例如 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 96153
the 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
PHP 提供了一个名为
strtr
的函数,它可以进行字符串翻译。第一个参数是要翻译的内容,第二个参数是原始字符,第三个参数是替换字符。下面是一个应该可以完成您想要的功能的函数。 编辑:更新了我的示例以删除任何不受支持的字符(除了 az 或空格之外的任何字符)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)像这样的结构怎么样...注意:这将忽略无效的“字母”,如空格、标点符号等。
现场演示 http://codepad.org/pQHGhm7Y
How about a structure like this... NOTE: This will ignore invalid 'letters' like spaces, punctuation, etc..
LIVE DEMO http://codepad.org/pQHGhm7Y
我不确定我是否完全理解你在问什么:
对于情况(1),很简单,只需使用一个数组将字母表中的每个字母映射到相应的数字(即键是字母,值是数字)。然后,您可以迭代输入的字符,并使用数组中相应的元素进行输出。
情况(2)有点棘手。您可以从名称列表中构建一个Trie,并用它来进行查找。
I'm not sure I understand exactly what you are asking:
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.