Php morse code decode a texto y viceversa 
Tuesday, July 31, 2012, 06:50 PM - Php
php morse para codificar y decodificar morse ya sea morse - texto o texto - morse , tiene problema con la letra Ñ





morse.php

<?php
/*
* Ben LeMasurier (c) 2002.
* Version 0.3
*
* phpMorse.php
* convert text to morse and vise versa
*
*
* LICENSE:
****
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
***
*
*
* uidzer0@uidzer0.org
*
**/
$txt = $_POST['txt'];
$morse = $_POST['morse'];

$charstodots=array();
$charstodots["a"]=".-";
$charstodots["b"]="-...";
$charstodots["c"]="-.-.";
$charstodots["d"]="-..";
$charstodots["e"]=".";
$charstodots["f"]="..-.";
$charstodots["g"]="--.";
$charstodots["h"]="....";
$charstodots["i"]="..";
$charstodots["j"]=".---";
$charstodots["k"]="-.-";
$charstodots["l"]=".-..";
$charstodots["m"]="--";
$charstodots["n"]="-.";
$charstodots["o"]="---";
$charstodots["p"]=".--.";
$charstodots["q"]="--.-";
$charstodots["r"]=".-.";
$charstodots["s"]="...";
$charstodots["t"]="-";
$charstodots["u"]="..-";
$charstodots["v"]="...-";
$charstodots["w"]=".--";
$charstodots["x"]="-..-";
$charstodots["y"]="-.--";
$charstodots["z"]="--..";
// upper
$charstodots["A"]=".-";
$charstodots["B"]="-...";
$charstodots["C"]="-.-.";
$charstodots["D"]="-..";
$charstodots["E"]=".";
$charstodots["F"]="..-.";
$charstodots["G"]="--.";
$charstodots["H"]="....";
$charstodots["I"]="..";
$charstodots["J"]=".---";
$charstodots["K"]="-.-";
$charstodots["L"]=".-..";
$charstodots["M"]="--";
$charstodots["N"]="-.";
$charstodots["O"]="---";
$charstodots["P"]=".--.";
$charstodots["Q"]="--.-";
$charstodots["R"]=".-.";
$charstodots["S"]="...";
$charstodots["T"]="-";
$charstodots["U"]="..-";
$charstodots["V"]="...-";
$charstodots["W"]=".--";
$charstodots["X"]="-..-";
$charstodots["Y"]="-.--";
$charstodots["Z"]="--..";
// num
$charstodots["1"]=".----";
$charstodots["2"]="..---";
$charstodots["3"]="...--";
$charstodots["4"]="....-";
$charstodots["5"]=".....";
$charstodots["6"]="-....";
$charstodots["7"]="--...";
$charstodots["8"]="---..";
$charstodots["9"]="----.";
$charstodots["0"]="-----";
$charstodots[" "]=" ";
$charstodots["."]=".-.-.-";
$charstodots[","]="--..--";
$charstodots["EOM"]=".-.-.";

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

function morse_encode($txt) {
global $charstodots;

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

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

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

function morse_decode($string) {
global $dotstochars;

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

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

<h1>phpMorse - morsecode encoder/decoder</h1>
by <a href="http://uidzer0.org" title="uidzer0.org">Ben LeMasurier</a>
<br />

<form method="post" action="<?php echo $PHP_SELF ?>">
Text: <input type="text" name="txt" value="<?php echo $txt?>" size=50>
<br>
Morse: <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 ($charstodots[$letter]=="") continue;

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

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

echo "$line1\n";
echo "$line2\n";
echo "\n";
echo "\n</pre>\n";
}
?>


Comentarios

Agregar comentario

Rellene los campos de abajo para dejar su comentario.









Extras (Negrita / Cursiva / URL / Imagen):








En este blog está activada la moderación. Tu comentario requiere que los administradores lo aprueben antes de hacerse visible.