停留在 php 莫尔斯解码器

stuck on php morse decoder

我正在为学校制作 php 莫尔斯编码器和解码器。
我已经让编码器工作了,但我不知道如何让解码器工作。

下面的代码是我的解码器。
我试着做通过用代码切换字母,解码器与我的编码器相同。 那没有用,因为它会混淆莫尔斯字母并将它们合并。

所以我想我必须首先获取放入解码器中的每个字母,将它们放入数组中并解码将它们分开,然后在最后将它们放回原处。

我试了很多东西,但我真的不知道自己在做什么,也想不通。
我是 php 的新手,所以我尽量保持简单。
感谢您的帮助!

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

这是个有趣的问题。我把它分成两个功能,一个编码器和一个解码器。它们都有相同的数组,这是我们从 get_morse() 函数中得到的(我只是将其拆分为一个单独的函数,因此行数更少)。 get_morse() 在每个摩尔斯电码后都有一个 space,因此在编码时不会混乱。我们可以很容易地 trim 在解码时用 trim() 去掉它(因为我们用 space 分割字符串以获得每个代码代表的字母)。

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" => "--** ");
}

那么编码器就很简单了,

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

它基本上用每个相应的莫尔斯电码(这是数组中的值)替换每个字母(这是莫尔斯数组中的键)。所以你在数组中得到'a' => "*- ",并用数组中的值替换所有字母(键)。

解码器可能可以通过一些 array_* 函数进一步简化,但这将正常工作并解码。我们将使用与之前相同的数组,来自 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);
}

我们将编码字符串拆分为 spaces,这为我们提供了一个数组,其中每个单独的摩尔斯电码都是其自身的元素。然后我们可以遍历它,'reverse' 我们在编码时所做的程序;找到数组中与摩尔斯电码匹配的每个元素,并用它的键替换它;这是合适的字母。

这些函数的用法是

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

Live demo