1

i was working today then i got this idea. I want to make a script that take a plain text and then encrypt it with T9 mode for SMS messages on mobile phone.

plain text : Hello

Result : 4433555555666

So i created some class like this :

<?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');
?>

the problem is i need some algorithms and lines on how i can compare every character from the given plain text with the keys array and then return a key number if there a match.

Alex
  • 89
  • 7

2 Answers2

2

You can always make this simpler, since you write it only once, you can use this:

$keys = array(
'a' => '2',
'b' => '22',
'c' => '222'... //and so on

and then use simple string replace.

Otherwise you can continue your code, but I think it will be not very optimized for performance, since you will need to loop through your array multiple times to find the given char.

Shir Gans
  • 1,976
  • 3
  • 23
  • 40
  • this goes against code optimization but i think it's okay, i'll start with this to make it works after that i will dosome changes xD – Alex Sep 08 '19 at 13:48
2
<?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');
?>
MimoudiX
  • 612
  • 3
  • 16