4

I'm making a php morse encoder and decoder for school.
i already got the encoder working but i can't figure out how to make the decoder work.

the code below is what i have for my decoder.
i tried to make the decoder the same as my encoder by switching the letters with the codes. That didn't work because it would confuse the morse letters and merge them.

so i think i have to first get each letter that was put in the decoder, put them in an array and decode them seperately and then put them back together at the end.

i tried a bunch of things, but really i don't know what i'm doing and i can't figure it out.
i'm new to php so i'm trying to keep it as simple as possible.
Any help is appreciated!

<?php
$break = "<br>" ;
$Words = $_POST['morse2'];

//get the morse, seperate each letter and then decode and echo the outcome

$seperation = explode(" ", $Words);
var_dump($seperation) ;
echo $break ;

// foreach($seperation){
// str_replace("/ "," ",$seperation);
// }
$seperation = str_replace("/ "," ",$Words);
$seperation = str_replace(".- ","a",$Words);
$seperation = str_replace("-... ","b",$Words);
$seperation = str_replace("-.-. ","c",$Words);
$seperation = str_replace("-.. ","d",$Words);
$seperation = str_replace(". ","e",$Words);
$seperation = str_replace("..-. ","f",$Words);
$seperation = str_replace("--. ","g",$Words);
$seperation = str_replace(".... ","h",$Words);
$seperation = str_replace(".. ","i",$Words);
$seperation = str_replace(".--- ","j",$Words);
$seperation = str_replace("-.- ","k",$Words);
$seperation = str_replace(".-.. ","l",$Words);
$seperation = str_replace("-- ","m",$Words);
$seperation = str_replace("-. ","n",$Words);
$seperation = str_replace("--- ","o",$Words);
$seperation = str_replace(".--. ","p",$Words);
$seperation = str_replace(".-. ","r",$Words);
$seperation = str_replace("... ","s",$Words);
$seperation = str_replace("- ","t",$Words);
$seperation = str_replace("..- ","u",$Words);
$seperation = str_replace("...- ","v",$Words);
$seperation = str_replace("-.-- ","y",$Words);
$seperation = str_replace("--.. ","z",$Words);
$seperation = str_replace("--.- ","q",$Words);
$seperation = str_replace(".-- ","w",$Words);
$seperation = str_replace("-..- ","x",$Words);

$seperation = str_replace(".---- ","1",$Words);
$seperation = str_replace("..--- ","2",$Words);
$seperation = str_replace("...-- ","3",$Words);
$seperation = str_replace("....- ","4",$Words);
$seperation = str_replace("..... ","5",$Words);
$seperation = str_replace("-.... ","6",$Words);
$seperation = str_replace("--... ","7",$Words);
$seperation = str_replace("---.. ","8",$Words);
$seperation = str_replace("----. ","9",$Words);
$seperation = str_replace("----- ","0",$Words);

echo $seperation;

  /*Here's a tiny piece of my encoder to show you how i did that
    basically the same as the decoder, real simple.
  $Words = $_POST['morse'];
  $Words = str_replace(" ","/ ",$Words);
  $Words = str_replace("a",".- ",$Words);
  $Words = str_replace("b","-... ",$Words);
  $Words = str_replace("c","-.-. ",$Words);
  echo $Words;
  /*
?>
mango
  • 55
  • 4
  • 2
    this is an interesting project. what are you being passed in `$_POST['morse2']` ? – bowl0stu Apr 04 '17 at 13:07
  • 1
    There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue which can be answered in a few paragraphs. I would suggest you find a development forum (perhaps [Quora](http://www.quora.com/Computer-Programming)?) to work out generalities. Then, when/if you have specific coding issues, come back to Stack Overflow and we'll be glad to help. – Jay Blanchard Apr 04 '17 at 13:08
  • 1
    *"so i think i have to first get each letter that was put in the decoder, put them in an array and decode them seperately and then put them back together at the end."* This sounds like the perfect approach. What failed when you tried this? – Jay Blanchard Apr 04 '17 at 13:09
  • Well as you said, i'm pretty sure it would work but i don't know how to do it... I know how to put them in an array, but i have no clue how you then take each letter from the array and decode them – mango Apr 04 '17 at 13:12

1 Answers1

6

Its an interesting question. I've split it into two functions, an encoder and a decoder. They both have the same array, which we get from a get_morse() function (I just split this into a separate function so it makes fewer lines). The get_morse() has a space after each morse-code, so that its not cluttered when you encode it. We can easily trim that away with trim() when decoding it (as we split the string by the spaces to get the letter represented by each code).

function get_morse() {
    return array(" " => "/ ", "a" => "*- ", "b" => "-*** ", "c" => "-*-* ", "d" => "-** ", "e" => "* ", "f" => "**-* ", "g" => "--* ", "h" => "**** ", "i" => "** ", "j" => "*--- ", "k" => "-*- ", "l" => "*-** ", "m" => "-- ", "n" => "-* ", "o" => "--- ", "p" => "*--* ", "q" => "--*- ", "r" => "*-* ", "s" => "*** ", "t" => "- ", "u" => "**- ", "v" => "***- ", "w" => "*-- ", "x" => "-**- ", "y" => "-*-- ", "z" => "--** ");
}

Then the encoder is very simple,

function morse_encoder($word) {
    return str_replace(array_keys(get_morse()), get_morse(), strtolower($word));
}

It basically replaces each letter (which is a key in the morse-array) with each respective morse-code (which is the value in the array). So you get 'a' => "*- " in the array, and replace all letters (keys) by the value in the array.

The decoder can probably be simplified even further with some array_* functions, but this will work and decode it properly. We'll use the same array as before, coming from get_morse().

function morse_decoder($word) {
    $morse = array_map("trim", get_morse());
    $output = "";
    foreach (explode(" ", $word) as $value) {
        $output .= array_search($value, $morse);
    }
    return strtoupper($output);
}

We split the encoded string by spaces, this gives us an array with each individual morse code as an element of its own. We can then loop over this, and 'reverse' the procedure we did when encoding; finding each element in the array matching the morse code, and replace that with its key; which is the appropriate letter.

Usage of these functions would be

echo morse_encoder("sos");          // Output: *** --- ***
echo morse_decoder("*** --- ***");  // Output: SOS

Live demo

Qirel
  • 25,449
  • 7
  • 45
  • 62