Skip to content

Instantly share code, notes, and snippets.

@CodeBrauer
Created August 17, 2023 09:39
Show Gist options
  • Save CodeBrauer/8c0fee94207edc4abbb9d62318a1055a to your computer and use it in GitHub Desktop.
Save CodeBrauer/8c0fee94207edc4abbb9d62318a1055a to your computer and use it in GitHub Desktop.
For a german t9 keypad. currently no checking for breaks between pressed characters.
<?php
declare(strict_types=1);
/* german T9 */
$str = '9335552224433 "2" 8444833555 4222 337777 9999886 5552886622244 3337777 6644466833663666 "64" 44466 3338887777222445552663?';
$chars = range('a', 'z');
$fourCharKeys = [7,9];
$decodingTable = [];
$charPosition = 0;
foreach (range(2, 9) as $number) {
$numberCharCount = in_array($number, $fourCharKeys) ? 4 : 3;
for ($i=1; $i <= $numberCharCount; $i++) {
$decodingTable[$chars[$charPosition++]] = str_repeat((string)$number, $i);
}
}
// replace "multiple-pressed button" letters first
arsort($decodingTable);
echo str_replace(array_values($decodingTable), array_keys($decodingTable), $str);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment