-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
126 lines (116 loc) · 4.84 KB
/
index.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
<?php
require_once 'components/nav.php';
require_once 'components/footer.php';
require_once 'components/wordlist.php';
//-----------------------------------------------------------------------------------------------------------
$body = 'please enter any combination of characters (under 8 characters)';
$letterArray = array();
$permuteArray = array();
$resultArray = array();
$input = "";
// ----------------------------------------------------------------------------------------------------------
if (isset($_POST['permute'])) {
$letterArray = str_split(strtolower(str_replace(' ', '', $_POST['permute'])));
$permuteArray = Cleaner(AllPermutations($letterArray));
$resultArray = CompareWords($wordlist, $permuteArray, count($letterArray));
$body = "";
$input = str_replace(' ', '', $_POST['permute']);
foreach ($resultArray as $value) {
if (strrev($value) == $value) {
$body .= "<div class='col m-2 d-flex flex-column align-items-center border'><a href='https://www.dictionary.com/browse/{$value}'>{$value}</a> (palindrome)</div>";
} else {
$body .= "<div class='col m-2 d-flex flex-column align-items-center border'><a href='https://www.dictionary.com/browse/{$value}'>{$value}</a></div>";
}
}
}
//----------------------------------FUNCTIONS----------------------------------------------------------------
// compares string to wordlist
function CompareWords($wordlist, $permuteArray, $letterCount) {
$returnArray = array();
for ($count=$letterCount;$count>2;$count--){
for ($i=0;$i<count($permuteArray);$i++) {
$compareWord = substr($permuteArray[$i],0,$count);
$tempWordArray = explode(PHP_EOL, $wordlist[mb_substr($permuteArray[$i], 0, 1)]);
for ($j=0;$j<count($tempWordArray);$j++) {
if ($tempWordArray[$j] == $compareWord) {
if (in_array($tempWordArray[$j], $returnArray)) {
continue;
} else {
$returnArray[] = $tempWordArray[$j];
}
} else {
continue;
}
}
}
}
return $returnArray;
}
// makes string out of permutation array for comparison
function Cleaner($permuteArray) {
$returnArray = array();
for ($i=0;$i<count($permuteArray);$i++) {
$returnArray[] = implode($permuteArray[$i]);
};
return $returnArray;
}
// finds all permutations of given array (string from input has to get converted into array before)
function AllPermutations($inArray, $inProcessedArray = array())
{
$returnArray = array();
foreach($inArray as $key=>$value)
{
$copyArray = $inProcessedArray;
$copyArray[$key] = $value;
$tempArray = array_diff_key($inArray, $copyArray);
if (count($tempArray) == 0)
{
$returnArray[] = $copyArray;
}
else
{
$returnArray = array_merge($returnArray, AllPermutations($tempArray, $copyArray));
}
}
return $returnArray;
}
?>
<!-- --------------------------------------PHP END---------------------------------------------------------------- -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php require_once "components/boot.php" ?>
<link rel="stylesheet" href="styles/styles.css">
<title>StefanKanta</title>
</head>
<body>
<?= $navbar ?>
<div class="d-flex flex-column align-items-center">
<h3 class="my-1">Wordfinder:</h1>
<div class="mb-3">(wordlist is not complete, ~70k words)</div>
<div class="container">
<div class="row row-cols-1 row-cols-md-2">
<div class='col d-flex flex-column align-items-center'>
<form action="<?= htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST" class="mx-5 d-flex flex-column align-items-center" enctype="multipart/form-data">
<label for='permute' class='form-label mb-1'>Insert string</label>
<input class='form-control py-1' type='text' name='permute' id='permute' placeholder='Insert String'>
<input class="btn btn-sm btn-success mt-3 mx-5" type="submit" name="submit" value="GO">
</form>
</div>
<div class='col d-flex flex-column align-items-center'>
<div class="row row-cols-1 row-cols-md-5">
<h5>Input: <?= $input ?></h5>
</div>
<div class="row d-flex justify-content-between flex-wrap">
<?= $body ?>
</div>
</div>
</div>
</div>
</div>
<?= $footer ?>
</body>
</html>