BigToach.com

PHP Snippets

morsecode

Information

(PHP Version >= 4.1.0)
Functions used in this snippet: strtolower, strlen, array_key_exists, explode, array_search, str_replace.

Description

str morsecode( str str[, bool fromalse] )

Takes str and converts it into morse code. If the optional from is set to true, the str will changed from morse code to plaintext.

Snippet

<?php
function morsecode($str,$from=false){
    
$morse = 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' => '-----'
                    
'.' => '·-·-·-'
                    
',' => '--··--'
                    
'?' => '··--··'
                    
"'" => '·----·'
                    
'!' => '-·-·--'
                    
'/' => '-··-·'
                    
'(' => '-·--·-'
                    
')' => '-·--·-'
                    
':' => '---···'
                    
';' => '-·-·-·'
                    
'=' => '-···-'
                    
'-' => '-····-'
                    
'_' => '··--·-'
                    
'"' => '·-··-·'
                    
'@' => '·--·-·');
    
$end '';
    
$str strtolower($str);
    if(!
$from){
        for(
$i=0$i<strlen($str); $i++){
            
$end .= array_key_exists($str{$i}, $morse) ? $morse[$str{$i}] . ' ' '';
        }
    }else{
        
$code explode(' '$str);
        foreach(
$code as $value){
            
$key array_search($value$morse);
            
$end .= $key === false '' $key;
            
$end .= $value === '' ' ' '';
        }
        
$end str_replace('    '' '$end);
    }
    return 
$end;
}
?>

Example

morsecode Can be used in the following way:

<?php
echo morsecode('oh my goodness its morsecode!!');
// --- ····     -- -·--     --· --- --- -·· -· · ··· ···     ·· - ···     -- --- ·-· ··· · -·-· --- -·· · -·-·-- -·-·-- 

echo morsecode('--- ····     -- -·--     --· --- --- -·· -· · ··· ···     ·· - ···     -- --- ·-· ··· · -·-· --- -·· · -·-·-- -·-·-- 'true);
// oh my goodness its morsecode!!
?>

Added on Jul 8th at 1 am by Scott - 0 Comments


0 Responses to morsecode

Name:

Site or Mail:

Comment:

To try to reduce the ammount of spam, please enter the word GOOSE in the following form

Security Code: