Skip to content

Instantly share code, notes, and snippets.

@vxzry
Last active March 20, 2020 02:46
Show Gist options
  • Save vxzry/a19ba3132bebd4b2e5a51fb82137a6bc to your computer and use it in GitHub Desktop.
Save vxzry/a19ba3132bebd4b2e5a51fb82137a6bc to your computer and use it in GitHub Desktop.
PHP code to convert morse code into english text and vice versa - challenge from https://github.com/ponchog/phpcodingchallenge
<?php
/**
* Converts morse code into english text and vice versa
* php version 7.2
*
* @category NA
* @package NA
* @author vzxry <vxzry@github.com>
*/
namespace MorseConverter;
class MorseConverter
{
const MORSECODES = array(
array('char'=>'0', 'morse'=>"-----"),
array('char'=>'1', 'morse'=>".----"),
array('char'=>'2', 'morse'=>"..---"),
array('char'=>'3', 'morse'=>"...--"),
array('char'=>'4', 'morse'=>"....-"),
array('char'=>'5', 'morse'=>"....."),
array('char'=>'6', 'morse'=>"-...."),
array('char'=>'7', 'morse'=>"--..."),
array('char'=>'8', 'morse'=>"---.."),
array('char'=>'9', 'morse'=>"----."),
array('char'=>'A', 'morse'=>".-"),
array('char'=>'B', 'morse'=>"-..."),
array('char'=>'C', 'morse'=>"-.-."),
array('char'=>'D', 'morse'=>"-.."),
array('char'=>'E', 'morse'=>"."),
array('char'=>'F', 'morse'=>"..-."),
array('char'=>'G', 'morse'=>"--."),
array('char'=>'H', 'morse'=>"...."),
array('char'=>'I', 'morse'=>".."),
array('char'=>'J', 'morse'=>".---"),
array('char'=>'K', 'morse'=>"-.-"),
array('char'=>'L', 'morse'=>".-.."),
array('char'=>'M', 'morse'=>"--"),
array('char'=>'N', 'morse'=>"-."),
array('char'=>'O', 'morse'=>"---"),
array('char'=>'P', 'morse'=>".--."),
array('char'=>'Q', 'morse'=>"--.-"),
array('char'=>'R', 'morse'=>".-."),
array('char'=>'S', 'morse'=>"..."),
array('char'=>'T', 'morse'=>"-"),
array('char'=>'U', 'morse'=>"..-"),
array('char'=>'V', 'morse'=>"...-"),
array('char'=>'W', 'morse'=>".--"),
array('char'=>'X', 'morse'=>"-..-"),
array('char'=>'Y', 'morse'=>"-.--"),
array('char'=>'Z', 'morse'=>"--.."),
array('char'=>".", 'morse'=>".-.-.-"),
array('char'=>",", 'morse'=>"--..--"),
array('char'=>"?", 'morse'=>"..--.."),
array('char'=>"!", 'morse'=>"-.-.--"),
array('char'=>"/", 'morse'=>"-..-."),
array('char'=>"(", 'morse'=>"-.--.-"),
array('char'=>")", 'morse'=>"-.--.-"),
array('char'=>"&", 'morse'=>".-..."),
array('char'=>":", 'morse'=>"---..."),
array('char'=>";", 'morse'=>"-.-.-."),
array('char'=>"=", 'morse'=>"-...-"),
array('char'=>"+", 'morse'=>".-.-."),
array('char'=>"-", 'morse'=>"-....-"),
array('char'=>"_", 'morse'=>"..--.-"),
array('char'=>"\"",'morse'=>".-..-."),
array('char'=>"$", 'morse'=>"...-..-"),
array('char'=>"@", 'morse'=>".--.-."));
/**
* Converts input into Morse code
*
* @param $input string
*
* @return string output
*/
public static function toMorse($input)
{
$morse = array_column(MorseConverter::MORSECODES, 'char', 'morse');
$result = '';
forEach (str_split($input) as $char) {
$morseSearch = array_search(strtoupper($char), $morse);
if ($morseSearch) {
$result = $result.$morseSearch.' ';
}
if ($char == ' ') {
$result = $result.'/';
}
}
return $result;
}
/**
* Converts input from Morse code to English text
*
* @param $input string
*
* @return string output
*/
public static function fromMorse($input)
{
$morse = array_column(MorseConverter::MORSECODES, 'morse', 'char');
$result = '';
forEach (explode(' ', $input) as $char) {
if ($char != '') {
if (substr($char[0], 0, 1) == '/') {
$result = $result.' ';
$char = substr($char, 1);
}
$morseSearch = array_search($char, $morse);
if ($morseSearch) {
$result = $result.$morseSearch;
}
}
}
return $result;
}
}
@vxzry
Copy link
Author

vxzry commented Feb 18, 2020

TODO: Comments hahaha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment