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;
/*
?>