#native_company# #native_desc#
#native_cta#

Morse Coder

By Edwin Groothuis
on August 21, 2001

Version: 1.0

Type: Function

Category: Algorithms

License: BSD License

Description: Morse-code encoder/decoder

<?php

    //
    // Morse Convertor v1.0 by Edwin Groothuis ([email protected])
    //
    // If you didn't get this file via http://www.mavetju.org, please
    // check for the availability of newer versions.
    //
    // See LICENSE for distribution issues. If this file isn't in
    // the distribution, please inform me about it.
    //
    // Feel free to use this yourself.
    //

    $lettertomorse=array();
    $lettertomorse["a"]=".-";
    $lettertomorse["b"]="-...";
    $lettertomorse["c"]="-.-.";
    $lettertomorse["d"]="-..";
    $lettertomorse["e"]=".";
    $lettertomorse["f"]="..-.";
    $lettertomorse["g"]="--.";
    $lettertomorse["h"]="....";
    $lettertomorse["i"]="..";
    $lettertomorse["j"]=".---";
    $lettertomorse["k"]=".-.";
    $lettertomorse["l"]=".-..";
    $lettertomorse["m"]="--";
    $lettertomorse["n"]="-.";
    $lettertomorse["o"]="---";
    $lettertomorse["p"]=".--.";
    $lettertomorse["q"]="--.-";
    $lettertomorse["r"]=".-.";
    $lettertomorse["s"]="...";
    $lettertomorse["t"]="-";
    $lettertomorse["u"]="..-";
    $lettertomorse["v"]="...-";
    $lettertomorse["w"]=".--";
    $lettertomorse["x"]="-..-";
    $lettertomorse["y"]="-.--";
    $lettertomorse["z"]="--..";
    $lettertomorse["1"]=".----";
    $lettertomorse["2"]="..---";
    $lettertomorse["3"]="...--";
    $lettertomorse["4"]="....-";
    $lettertomorse["5"]=".....";
    $lettertomorse["6"]="-....";
    $lettertomorse["7"]="--...";
    $lettertomorse["8"]="---..";
    $lettertomorse["9"]="----.";
    $lettertomorse["0"]="-----";
    $lettertomorse[" "]="   ";
    $lettertomorse["."]=".-.-.-";
    $lettertomorse[","]="--..--";
    $lettertomorse["EOM"]=".-.-.";

    $morsetoletter=array();
    reset($lettertomorse);
    while (list($letter,$code)=each($lettertomorse)) {
	$morsetoletter[$code]=$letter;
    }

    function morse_encode($txt) {
	global $lettertomorse;

	$line="";
	for ($i=0;$i<strlen($txt);$i++) {
	    $letter=substr($txt,$i,1);

	    // ignore unknown characters
	    if ($lettertomorse[$letter]=="") continue;

	    $line.=$lettertomorse[$letter]." ";
	}
	return $line;
    }

    function morse_decode($string) {
	global $morsetoletter;

	$line="";
	$letters=array();
	$letters=explode(" ",$string);
	foreach ($letters as $letter) {
	    // ignore unknown characters
	    if ($letter=="") $line.=" ";
	    if ($morsetoletter[$letter]=="") continue;

	    $line.=$morsetoletter[$letter];
	}
	return $line;
    }
?>

<h1>Morse transletter</h1>

<form method="post" action="<?php echo $PHP_SELF ?>">
Real life text: <input type="text" name="txt" value="<?php echo $txt?>" size=50>
<br>
Morse text: <input type="text" name="morse" value="<?php echo $morse?>" size=50>
<input type="submit" value="Convert">
<input type="reset">
<br>
<hr>

<?php

    if ($morse!="") {
	echo morse_decode($morse),"<br>n";
    }

    if ($txt!="") {
	echo morse_encode($txt),"<br>n";
    }

    if ($txt!="") {
	echo "<hr>n<pre>";

	$line1="";
	$line2="";

	for ($i=0;$i<strlen($txt);$i++) {
	    $letter=substr($txt,$i,1);

	    // ignore unknown characters
	    if ($lettertomorse[$letter]=="") continue;

	    $line1.=$lettertomorse[$letter]." ";
	    $line2.=$letter;
	    for ($j=0;$j<strlen($lettertomorse[$letter]);$j++)
		$line2.=" ";
	    $size+=strlen($lettertomorse[$letter])+1;

	    if ($size>70) {
		echo "$line1n";
		echo "$line2n";
		echo "n";
		$line1="";
		$line2="";
		$size=0;
	    }
	}

	echo "$line1n";
	echo "$line2n";
	echo "n";
	echo "n</pre>n";
    }
?>