Skip to content

Instantly share code, notes, and snippets.

@bitifet
Last active December 18, 2015 13:18
Show Gist options
  • Save bitifet/5788625 to your computer and use it in GitHub Desktop.
Save bitifet/5788625 to your computer and use it in GitHub Desktop.
Funny utility to encode and (yet buggy) decode strings into morse.
#!/usr/bin/env php
<?php
class morse {/*{{{*/
private $a2m;
private $m2a;
private $sep;
private $lang;
public function __construct(/*{{{*/
$lang = 'foo',
$sep = null
) {
list ($lang) = explode ('_', $lang);
$this->lang = strtolower($lang); // For 'es' => CH — — — — (Marcaespaña...)
$this->sep = is_null($sep) ? '/' : $sep; // FIXME: Double space doesn't work well yet on decoding, single is a disaster...
$this->a2m = array (/*{{{*/
'A' => '·—',
'B' => '—···',
'CH' => '————',
'C' => '—·—·',
'D' => '—··',
'E' => '·',
'F' => '··—·',
'G' => '——·',
'H' => '····',
'I' => '··',
'J' => '·———',
'K' => '—·—',
'L' => '·—··',
'M' => '——',
'N' => '—·',
'Ñ' => '——·——',
'O' => '———',
'P' => '·——·',
'Q' => '——·—',
'R' => '·—·',
'S' => '···',
'T' => '—',
'U' => '··—',
'V' => '···—',
'W' => '·——',
'X' => '—··—',
'Y' => '—·——',
'Z' => '——··',
'0' => '—————',
'1' => '·————',
'2' => '··———',
'3' => '···——',
'4' => '····—',
'5' => '·····',
'6' => '—····',
'7' => '——···',
'8' => '———··',
'9' => '————·',
'.' => '·—·—·—',
',' => '——··——',
'?' => '··——··',
'"' => '·—··—·',
'!' => '—·—·——',
' ' => ' ',
);/*}}}*/
$this->m2a = array_flip($this->a2m);
}/*}}}*/
public function encode ($str) {/*{{{*/
$str = preg_replace (
'/(.)/u',
"{$this->sep}\\1",
strtoupper(trim ($str))
);
if ($this->lang == 'es') {
$str = str_replace (
"C{$this->sep}H",
'CH',
$str
);
};
$str = str_replace (
array_keys ($this->a2m),
array_values ($this->a2m),
$str
);
return trim ($str, $this->sep);
}/*}}}*/
public function decode ($str) {/*{{{*/
$str = explode (
$this->sep,
trim($str)
);
foreach (
$str
as $i => $m
) $str[$i] = is_null ($a = @ $this->m2a[$m]) ? $m : $a;
$str = implode ('', $str);
return $str;
}/*}}}*/
};/*}}}*/
// Handle command line:/*{{{*/
$cmdstr = array_shift($argv); // Discard file name.
$opts = array();
foreach (
$argv
as $i => $token
) {
if (preg_match(
'/^-(.*?)(?:=(.*))?$/',
$token,
$matches
)) {
$opts[strtolower($matches[1])] = strlen($value = @ $matches[2]) ? $value : true; // Capture.
unset ($argv[$i]); // Discard.
} else break; // Modifiers only at beginning.
};
$string = implode (' ', $argv); // Use the rest as input string.
/*}}}*/
// Help:
if (
@ $opts['help']
|| @ $opts['h']
|| @ $opts['?']
|| ! strlen ($string)
) {
echo "NAME\n"
." Encode string into morse and (buggy) try to decode it again.\n"
."\n"
. "SYNOPSIS\n"
." {$cmdstr} [options] <string>\n"
."\n"
. "DESCRIPTION\n"
." Valid options:\n"
." -h Show this help message\n"
." -lang=<lang_code> Set input language (Only useful for es 'CH'...\n"
." -sep=<separator> Change character separator (decoding doesn't work well with spaces nowadays...)\n"
."\n";
die();
}
$m = new morse(
@ $opts['lang'],
@ strlen ($sep = $opts['sep']) ? $sep : null
);
$morsel = strlen ($morse = $m->encode ($string));
$asciil = strlen ($ascii = $m->decode ($morse));
echo "MORSE ({$morsel}): {$morse}\n";
echo "ASCII ({$asciil}): {$ascii}\n";
// vim: foldmethod=marker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment