« Mengajarkan Rukun Iman Kepada Anak | Home | UU Anti Pornografi Telah Disahkan Bagaimana Selanjutnya? »
Iseng Bikin Terjemahan Morse pake PHP
By Abu Aisyah | October 22, 2008
Ini adalah proyek isengku, karena ada seorang teman yang mengirim message pake bahasa morse, yah namanya juga iseng :P.
Aturan penulisan morse adalah misal: /…/…-/-./
Apabila ingin menjadikan morse ke huruf, maka spasi dianggap tidak ada. Misal : Saya Lapar . Maka akan dianggap SayaLapar.
Misalnya kita bikin form sederhana dulu, seperti ini:
<form method=‘post’ action=”>
<table border=‘1′ width=‘500′ cellspacing=‘0′ cellpadding=‘3′>
<tr>
<td>Mengubah</td>
<td><select name=‘option’>
<option value=‘A’ <? if ($_POST['option'] == “A”) echo “selected”; ?>>Morse - Huruf</option>
<option value=‘B’ <? if ($_POST['option'] == “B”) echo “selected”; ?>>Huruf - Morse</option>
</select>
</td>
</tr>
<tr>
<td colspan=‘2‘><strong>Input: </strong><input type=‘text’ name=‘isi’ maxlength=‘225′ size=‘50′ value=’<?=$_POST['isi']?>‘ /></td>
</tr>
<tr>
<td colspan=’2′><strong>Hasil Translate :</strong> <br/><span style=“font-size:24px;font-weight:bold;”><?=$hasil?></span></td>
</tr>
<tr>
<td colspan=‘2′ align=‘center’><input type=’submit’ name=’submit’ value=‘Ubah’ /></td>
</tr>
</table>
</form>
Berikutnya, saya membuat 2 variabel array. Yang pertama untuk menyimpan kode-kode morse, yang kedua untuk menyimpan huruf-huruf.
Misalnya seperti ini:
$arrMorse = array(
”/.-/”, “/-…/”, “/-.-./”, “/-../”, “/./”, “/..-./”, “/–./”, “/…./”, “/../”, “/.—/”, “/-.-/”, “/.-../”, “/–/”, “/-./”, “/—/”, “/.–./”, “/–.-/”, “/.-./”, “/…/”, “/-/”, “/..-/”, “/…-/”, “/.–/”, “/-..-/”, “/-.–/”, “/–../”
);
$arrHuruf = 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”
);
sedangkan untuk prosesnya pake koding berikut:
$hasil = “”;
if ($_POST['submit']){
if ($_POST['option'] == “A”){
$text = $_POST['isi'];
$count = strlen($text);
$hrfdepan = $text{0};
$hrfblkg = $text{$count-1};
$text = str_replace(“/”, “//”, $text);
$text = str_replace(” “, “//”, $text);
#echo $text;
$text = ($hrfblkg != “/”) ? $text.“/” : $text;
$text = ($hrfdepan != “/”) ? “/”.$text : $text;
$text = str_replace($arrMorse, $arrHuruf, $text);
$text = str_replace(“/”, “”, $text);
$hasil = $text;
}else{
$text = strtoupper($_POST['isi']);
$text = str_replace(” “, “”, $text);
$hasil = str_replace($arrHuruf, $arrMorse, $text);
$hasil = str_replace(“//”, “/”, $hasil);
}
}
Yaaah, ….namanya juga script simple, nggak mau ribet2 amat. Happy to use it. ![]()
Topics: Ngoprek, Open Source, PHP |

