Morse Code encoder/decoder

Discussion in 'PHP' started by pradeep, Feb 21, 2007.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Morse code is a method for transmitting information, using standardized sequences of short and long elements, for the letters, numerals, punctuation and special characters of a message. The short and long elements can be formed by sounds, marks or pulses, and are commonly known as "dots" and "dashes" or "dits" and "dahs". There are short spaces between the dits and dahs within a character, longer ones between characters, and still longer ones between words.

    Read more here

    PHP:
    $lettertomorse=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" => "--..",
         
    "1" => ".----",
         
    "2" => "..---",
         
    "3" => "...--",
         
    "4" => "....-",
         
    "5" => ".....",
         
    "6" => "-....",
         
    "7" => "--...",
         
    "8" => "---..",
         
    "9" => "----.",
         
    "0" => "-----",
         
    " " => "   ",
         
    "." => ".-.-.-",
         
    "," => "--..--",
         
    "EOM" => ".-.-."
         
    );
     
     
    $morsetoletter=array();
     
    reset($lettertomorse);
     foreach(
    $lettertomorse as $letter => $code
     {
         
    $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 (empty($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 (empty($letter)) 
                 
    $line.=" ";
             if (empty(
    $morsetoletter[$letter])) 
                 continue;
     
             
    $line.=$morsetoletter[$letter];
         }
         return 
    $line;
     }
     
    Example usage:
    PHP:
    print morse_encode("I Am Fine");
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice