我正在为学校制作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()在每个摩尔斯电码后都有一个空格,因此在进行编码时不会混乱.解码时,我们可以使用trim()轻松地将其修剪掉(因为我们将字符串除以空格以获取每个代码表示的字母).
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);
}
我们将编码的字符串用空格分开,这为我们提供了一个数组,其中每个摩尔斯电码都作为自己的元素.然后,我们可以遍历此过程,并“逆转”编码时执行的过程;在数组中找到与摩尔斯电码匹配的每个元素,并用其键替换它;这是适当的字母.
这些功能的用法是
echo morse_encoder("sos"); // Output: *** --- ***
echo morse_decoder("*** --- ***"); // Output: SOS