forked from iamcal/php-snippets
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmorse.php
More file actions
121 lines (104 loc) · 2.38 KB
/
morse.php
File metadata and controls
121 lines (104 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?
$fwd_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' => '--..',
'0' => '-----',
'1' => '.----',
'2' => '..---',
'3' => '...--',
'4' => '....-',
'5' => '.....',
'6' => '-....',
'7' => '--...',
'8' => '---..',
'9' => '----.',
'.' => '.-.-.-',
',' => '--..--',
'?' => '..--..',
':' => '---...',
"'" => '.----.',
'"' => '.-..-.',
'-' => '-....-',
'/' => '-..-.',
'(' => '-.--.',
')' => '-.--.-',
'Ä' => '.-.-',
'Á' => '.--.-',
'Å' => '.--.-',
'Ch' => '----',
'É' => '..-..',
'Ñ' => '--.--',
'Ö' => '---.',
'Ü' => '..--',
);
$rev_morse = array_flip($fwd_morse);
function text_to_morse($msg){
global $fwd_morse;
$msg = StrToUpper($msg);
$words = preg_split("/\s+/", $msg);
$words_out = array();
foreach($words as $word){
$bits = array();
for($i=0; $i<strlen($word); $i++){
$temp = $fwd_morse[substr($word,$i,1)];
if ($temp) $bits[] = $temp;
}
$words_out[] = implode(' ', $bits);
}
return implode(' / ', $words_out);
}
function morse_to_text($msg){
global $rev_morse;
#$msg = preg_replace("/[^.\/-]/", "", $msg);
$bits = preg_split("/\s+/", $msg);
$out = '';
foreach($bits as $bit){
if ($bit == '/'){
$out .= " ";
}else{
$out .= $rev_morse[$bit];
}
}
return $out;
}
$morse = StripSlashes($morse);
$text = StripSlashes($text);
if ($action == 't2m') $morse = text_to_morse($text);
if ($action == 'm2t') $text = morse_to_text($morse);
?>
<form action="morse.php" method="post">
<input type="hidden" name="action" value="t2m">
Text:<br>
<textarea cols="40" rows="6" name="text"><?=htmlentities($text)?></textarea><br>
<input type="submit" value="Text To Morse">
</form>
<form action="morse.php" method="post">
<input type="hidden" name="action" value="m2t">
Morse:<br>
<textarea cols="40" rows="6" name="morse"><?=htmlentities($morse)?></textarea><br>
<input type="submit" value="Morse To Text">
</form>