I built a form which asks you to write a word in German and it will tell which definite article you should use.
I used arrays from three questions:
Here is my code:
<form action="index.php" method="post">
<input type="text" class="form-control" id="word" name="word" placeholder="Word">
<button type="submit" class="btn btn-secondary">See</button>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
$word = $_POST["word"];
$das = ["Kind", "Licht", "Mädchen"];
$der = ["Hund", "Kater", "Mann", "Storm", "Winter"];
$die = ["Dame", "Frau", "Katze"];
sort($word);
sort($das);
sort($der);
sort($die);
if ($word == $das)
{
echo "It is a <b>das</b>.";
}
elseif ($word == $der)
{
echo "It is a <b>der</b>.";
}
else
{
echo "It is a <b>die</b>.";
}
}
?>
you can try this
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
$word = $_POST["word"];
$word=explode(" ",$word);
$das = ["Kind", "Licht", "Mädchen"];
$der = ["Hund", "Kater", "Mann", "Storm", "Winter"];
$die = ["Dame", "Frau", "Katze"];
$check1 = ($word == $das);
$check2 = ($word == $der);
if ($check1==true)
{
echo "It is a <b>das</b>.";
}
elseif ($check2==true)
{
echo "It is a <b>der</b>.";
}
else
{
echo "It is a <b>die</b>.";
}
}
?>
It seems you need the in_array
function and don’t need to sort
at all:
<form action="index.php" method="post">
<input type="text" class="form-control" id="word" name="word" placeholder="Word">
<button type="submit" class="btn btn-secondary">See</button>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
$word = $_POST["word"];
$das = ["Kind", "Licht", "Mädchen"];
$der = ["Hund", "Kater", "Mann", "Storm", "Winter"];
$die = ["Dame", "Frau", "Katze"];
if (in_array($word, $das))
{
echo "It is a <b>das</b>.";
}
elseif (in_array($word, $der))
{
echo "It is a <b>der</b>.";
}
elseif (in_array($word, $die))
{
echo "It is a <b>die</b>.";
}
}
?>
you can use this
$word = $_POST["word"];
$word=explode(" ",$word);
$das = ["Kind", "Licht", "Mädchen"];
$der = ["Hund", "Kater", "Mann", "Storm", "Winter"];
$die = ["Dame", "Frau", "Katze"];
$das = array_map('strtolower', $das);
$der = array_map('strtolower', $der);
$die = array_map('strtolower', $die);
$word = array_map('strtolower', $word);
Since this is something you're going to parse through often, store the words lowercased in your script. Don't use sort, and if you have a lot of words (and isn't going to use a database of some sort), store them as the array keys instead and use isset()
.
I've also taken the liberty to generalise the code that checks for the different types, so that you don't have to special case each type of word group further below (.. so in the future it'd be trivial to add new groups, such as detecting various forms of verbs).
To implement (and it's not enough to just check if the request method is POST - it's better to check if the POST var is set):
<form action="index.php" method="post">
<input type="text" class="form-control" id="word" name="word" placeholder="Word">
<button type="submit" class="btn btn-secondary">See</button>
</form>
<?php
if (!empty($_POST["word"]))
{
$word = strtolower($_POST["word"]); // be aware, this might not be locale aware for german letters. Look at IntlChar::toLower for proper support with UTF-8.
$dictionary = [
"das" => ["kind", "licht", "mädchen"],
"der" => ["hund", "kater", "mann", "storm", "winter"],
"die" => ["dame", "frau", "katze"];
];
$found = false;
foreach ($dictionary as $group => $words)
{
if (in_array($words, $word))
{
echo "It is a <b>" . $group . "</b>.";
$found = true;
break;
}
}
if (!$found)
{
echo "Not found.";
}
}