php t9 array str пиздец len mb_strlen str out for len mb_substr str fo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
$t9 = array(
0 => [' '],
1 => ['.', ',', '\'', '?', '!', '-', '&', ':'],
2 => ['а', 'б', 'в', 'г'],
3 => ['д', 'е', 'ж', 'з'],
4 => ['и', 'й', 'к', 'л'],
5 => ['м', 'н', 'о', 'п'],
6 => ['р', 'с', 'т', 'у'],
7 => ['ф', 'х', 'ц', 'ч'],
8 => ['ш', 'щ', 'ъ', 'ы'],
9 => ['ь', 'э', 'ю', 'я'],
);
$str = "пиздец";
$len = mb_strlen($str);
$out = "";
for ($i = 0; $i < $len; ++$i) {
$c = mb_substr($str, $i, 1);
foreach ($t9 as $n => $chars) {
$idx = array_search($c, $chars);
if ($idx !== false) {
$out .= str_repeat($n, $idx + 1)." ";
// echo "$n => ".$idx."\n";
break;
}
}
}
echo $out."\n";
$n = preg_match_all("/(\d+)/s", $out, $chars);
foreach ($chars[1] as $c) {
$idx = strlen($c) - 1;
echo $t9[(int) $c[0]][$idx];
}
echo "\n";