我今天工作,然后我有了这个想法。我想做一个脚本,采取一个纯文本,然后加密它与T9模式的短信在手机上。
纯文本: Hello
结果: 4433555555666
所以我创建了这样的类:
<?php
class decoder {
public $keys = array(
'2' => 'abc',
'3'=> 'def',
'4'=> 'ghi',
'5'=> 'jkl',
'6'=> 'mno',
'7'=> 'pqrs',
'8'=> 'tuv',
'9'=> 'wxyz',
'0'=> ' '
);
function key($string) {
// store every character from the string into an array
$str = str_split($string);
}
}
$deco = new decoder;
echo $deco->key('hello');
?>问题是,我需要一些算法和行,说明如何将给定纯文本中的每个字符与键数组进行比较,如果匹配,则返回一个键号。
发布于 2019-09-08 14:33:34
<?php
class decoder {
public $keys = array(
'a' => '2',
'b'=> '22',
'c'=> '222',
'd'=> '3',
'e'=> '33',
'f'=> '333',
'g'=> '4',
'h'=> '44',
'i'=> '444',
'j'=> '5',
'k'=> '55',
'l'=> '555',
'm'=> '6',
'n'=> '66',
'o'=> '666',
'p'=> '7',
'q'=> '77',
'r'=> '777',
's'=> '7777',
't'=> '8',
'u'=> '88',
'v'=> '888',
'w'=> '9',
'x'=> '99',
'y'=> '999',
'z'=> '9999',
' '=> '0'
);
function key($string) {
// store every character from the string into an array
$char = str_split($string);
$i = 0;
while ($i<count($char)) {
//foreach all keys and values
foreach ($this->keys as $key => $key_value) {
if ($char[$i] == $key) {
echo $key_value;
}
}
$i++;
}
}
}
$deco = new decoder;
echo $deco->key('hello');
?>发布于 2019-09-08 13:41:18
因为您只写了一次,所以您可以使用以下方法:
$keys = array(
'a' => '2',
'b' => '22',
'c' => '222'... //and so on然后使用简单的字符串替换。
否则,您可以继续您的代码,但我认为它将不是非常优化的性能,因为您将需要循环您的数组多次寻找给定的字符。
https://stackoverflow.com/questions/57842207
复制相似问题