Skip to content

Instantly share code, notes, and snippets.

@aurorapar
Created August 30, 2018 18:17
Show Gist options
  • Save aurorapar/0b1e2cb1a39967c6cd754a755a437c32 to your computer and use it in GitHub Desktop.
Save aurorapar/0b1e2cb1a39967c6cd754a755a437c32 to your computer and use it in GitHub Desktop.
DIctionary for SE
<?php
include 'index.txt';
include 'diclib.php';
$theParts = array('Noun','Adjective','Adverb','Conjunction','Pronoun','Verb','Preposition','Interjection');
$key = $parts = $definition = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if(!empty($_POST['newWord']))
{
$key = trim($_POST['newWord']);
foreach($theParts as $wordParts)
{
#echo "<br>Testing " . $wordParts . ' and it is ' . $_POST[$wordParts];
if(!empty($_POST[$wordParts]))
{
$tempPart = trim($_POST[$wordParts]);
$parts = $parts . $tempPart . ";";
}
}
if(strlen($parts) == 0)
{
exit("<p>You need at least one part of speech.<p>");
}
if(!empty($_POST['newDef']))
{
$definition = trim($_POST['newDef']);
addWord($key, $parts, $definition);
}
else
{
exit("<p>No definition specified.<p>");
}
}
else
{
exit("<p>No word specified.<p>");
}
}
?>
<?php
function searchForWord($key)
{
$myfile = fopen("dictionary.db", "r") or die("Unable to open file!");
// Output one line until end-of-file
$parts = "";
$definition = "";
while(!feof($myfile))
{
$current = explode(" ", fgets($myfile));
if($current[0] == $key)
{
$parts = $current[1];
for($x = 2; $x < sizeof($current); $x++)
{
$definition = $definition . $current[$x] . " ";
}
break;
}
}
fclose($myfile);
$results = array($key, $parts, $definition);
return $results;
}
function addWord($key, $parts, $definition)
{
$exists = searchForWord($key);
if(strlen($exists[1]) > 0)
{
echo "<p>Already exists<p>";
return False;
}
$myfile = fopen("dictionary.db", "a") or die("Unable to open file!");
$key = trim($key);
fwrite($myfile, $key . " " . $parts . " " . $definition);
fclose($myfile);
echo "<p>Word added!<p>" . "<p>" . $key . "<br>" . $parts . "<br>" . $definition;
return True;
}
function close()
{
#could probably use for a close function
echo "<script>window.close();</script>";
}
?>
<?php
include 'index.txt';
include 'diclib.php';
// define variables and set to empty values
$key = $parts = $definition = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if(!empty($_POST['word']))
{
$key = trim($_POST['word']);
$results = searchForWord($key);
if(strlen($results[1]) > 0)
{
echo '<p>Your word has an entry!<p>';
echo '<br>' . $key . '<br>';
$displayParts = explode(';', $results[1]);
foreach($displayParts as $eachPart)
{
echo $eachPart . ' ';
}
echo '<br>' . $results[2] . '<br>';
}
else
{
echo '<p>Word doesn\'t exist.<p>';
echo '<br>' . $key . '<br>';
}
}
else
{
echo "<p>No search submitted";
}
}
else
{
echo "<p>No search submitted";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment