-1

I want simplified implementation of T9 input method for mobile in PHP i.e. either using Trie or any other simple and best possible solution.

Any help will be highly appreciated.

  • 2
    It's nice that you want it. What is your question? What have you tried? – Antti29 Aug 19 '14 at 06:51
  • I want simple and best possible solution using PHP. I have used Trie. So i want to know is there any other solution or optimized Trie implementation. Hope you got what my question is... Thanks – BrightVision Aug 19 '14 at 07:01
  • 2
    I have a [Trie implementation](https://github.com/MarkBaker/Tries) in PHP, but while Tries are fast to search, they are slow to load, so it may not really be the solution you're looking for – Mark Baker Aug 19 '14 at 07:08
  • @MarkBaker Thanks for your suggestion. Yes i am aware of it that tries are slow to load and that what i want to overcome if i can optimize tries or with some other efficient solution. – BrightVision Aug 19 '14 at 07:23

1 Answers1

-1

It might not that efficient and there might be some better solution to your problem and its got some limitations as well e.g. if you enter number more than 15 digits it might not work properly. But I just tried to share my thoughts without using Tries it might gives you some idea. Just try and share your experience.

<?php

$T9Array = array(
    2 => array('a', 'b', 'c'),
    3 => array('d', 'e', 'f'),
    4 => array('g', 'h', 'i'),
    5 => array('j', 'k', 'l'),
    6 => array('m', 'n', 'o'),
    7 => array('p', 'q', 'r', 's'),
    8 => array('t', 'u', 'v'),
    9 => array('w', 'x', 'y', 'z')
);

function search_combination($input)
{
    global $T9Array;
    if (! is_numeric($input))
        return false;
    $arr = str_split($input);
    $total = 1;
    for($a = count($arr) - 1; $a >= 0; $a--)
    {
        $total *= count($T9Array[$arr[$a]]);
        $t[$a] = $total;
    }
    sort($t);
    for ($b = 0; $b < count($arr); $b++)
    {
        $k = $l = 0;
        $j = count($arr) - ($b + 2);
        for ($c = 0; $c < $total; $c++)
        {
            $ret[$c] .= $T9Array[$arr[$b]][$l];
            if ($j >= 0 && $c == ($t[$j] * ($k+1)) - 1 || $j < 0)
            {
                $k++;
                if ($l < count($T9Array[$arr[$b]]) - 1)
                    $l++;
                else
                    $l = 0;
            }
        }
    }
    return $ret;
}

function search_combination_str($string)
{
    global $T9Array;
    if (empty($string))
        return false;
    $arr = str_split(strtolower($string));
    foreach ($arr as $a)
    {
        foreach ($T9Array as $key => $val)
        {
            $tmp = array_keys($val, $a);
            if ($tmp)
                $conv .= $key;
        }
    }
    return search_combination($conv);
}

?>
Abbasi
  • 617
  • 7
  • 14
  • Thanks as you said there might be some better solution but it gives at-least some idea. will share my experience here. – BrightVision Aug 19 '14 at 08:39