/
jumbleMaker.php
212 lines (167 loc) · 4.48 KB
/
jumbleMaker.php
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://w...content-available-to-author-only...3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://w...content-available-to-author-only...3.org/1999/xhtml">
<head>
<title>Jumble Maker</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<pre>
<?php
define("NAME", "Bryce Pearce");
define("DESCRIPTION", "Day Number");
/* ------------------- Function: printHeader ----------- */
function printHeader()
{
static $sent = 0;
if (!$sent)
{
$sent = 1;
}
}
/* -------------------- Function: writeBeginningHTML ---- */
function writeBeginningHTML($heading)
{
printHeader();
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
. " \"http://w...content-available-to-author-only...3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"
. "<html xmlns=\"http://w...content-available-to-author-only...3.org/1999/xhtml\">"
. "<head>"
. "<title>" . NAME
. "</title>"
. "</head>"
. "<body>"
. "<h1>" . $heading . " </h1>\n";
}
/* -------------------- Function: writeAddress ---------- */
function writeAddress()
{
echo "<HR />"
. "<address>"
. "Time of Request: " . date("D j M Y h:i:s A") . "\n"
. "<a href=\"http://csnew.angelo.edu/~bpearce\">Bryce Pearce</a><br />"
. "<a href=\"mailto:bpearce@angelo.edu\">bpearce@angelo.edu</a><br />"
. "<br />\n"
. "</address>\n"
. "</body>\n"
. "</html>\n";
}
/* ------------------- Function: errorMsg -------------- */
function errorMsg($msg)
{
printHeader();
writeBeginningHTML(" Jumble Word Solver Error");
echo "Your form results could not be processed because ". $msg ;
echo "<HR />"
. "<address>"
. "Time of Request: " . date("D j M Y h:i:s A")
. "\n"
. "<a href=\"http://csnew.angelo.edu/~bpearce\">Bryce Pearce</a><br />"
. "<a href=\"mailto:bpearce@angelo.edu\">bpearce@angelo.edu</a><br />"
. "<br />\n"
. "</address>\n"
. "</body>\n"
. "</html>\n";
exit(1);
}
function permute($str)
{
/* If we only have a single character, return it */
if (strlen($str) < 2)
{
return array($str);
}
/* Initialize the return value */
$permutations = array();
/* Copy the string except for the first character */
$tail = substr($str, 1);
/* Loop through the permutations of the substring created above */
foreach (permute($tail) as $permutation)
{
/* Get the length of the current permutation */
$length = strlen($permutation);
/* Loop through the permutation and insert the first character of the original
string between the two parts and store it in the result array */
for ($i = 0; $i <= $length; $i++)
{
$permutations[] = substr($permutation, 0, $i) . $str[0] . substr($permutation, $i);
}
}
/* Return the result */
return $permutations;
}
printHeader();
$str = $_SERVER['QUERY_STRING'];
//checks for word=, before it trims
if (!preg_match('/word=/', $str))
{
errorMsg("variable word is missing.");
exit;
}
//set $str to be trimmed and continue
$str = ltrim($str,"word=");
//check for errrrrrrros
//if not enough or too many numbers
if(strlen($str) < 4 || strlen($str) > 7 || $str == "")
{
//if data after word= is empty
if($str == "")
{
errorMsg("variable word is empty.");
exit;
}
errorMsg("words must be at least four and at most seven letters long.<br />\n");
exit;
}
//check for non alphabetic characters
else if (!preg_match('/^[A-z]+$/', $str))
{
errorMsg("words must be only letters.");
exit;
}
//unJumbler, if no errors occured
writeBeginningHTML(NAME . " - " . COURSE . " - Lab " . LAB_NO . " Jumble Word Solver Results");
$word = strtoupper($str);
$perms = permute($word);
$data = file_get_contents("/usr/dict/words");
$data = strtoupper($data);
$lines = explode(PHP_EOL, $data);
$matches = array_intersect($perms, $lines);
$word = strtoupper($str);
$matches = array_unique($matches);
$count = count($matches);
$test=count($matches);
if($test == 1 || $test == 0)
{
if($test == 0)
{
echo "For the Jumble word $str, no solutions were found.<br />\n";
writeAddress();
exit;
}
else
{
echo "For the Jumble word $str, 1 solution was found.<br />";
echo $unJumble;
writeAddress();
exit;
}
}
echo "For the Jumble word <b>$word</b>, $count solutions were found.\n\n";
$i = 0;
echo "<b>";
foreach ($matches as $key => $word)
{
$i++;
echo "$i. $word\n";
}
echo "</b>";
echo "<br />\n";
echo $unJumble;
//footer
writeAddress();
?>
</pre>
</p>
</body>
</html>