0

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>.";
   }
 }

?>
Oo'-
  • 203
  • 6
  • 22
  • read about `in_array()` - http://php.net/manual/en/function.in-array.php – splash58 Oct 21 '17 at 16:17
  • Where is the question? What is expected? Why `sort($word)`? `sort()` is for arrays (http://php.net/manual/en/function.sort.php). Please improve the question – mloureiro Oct 21 '17 at 16:34
  • @mloureiro, because `sort` ignores the differences of case-insensitive among lowercase and uppercase same words. – Oo'- Oct 21 '17 at 17:51

4 Answers4

0

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>.";
    }
}

?>
mloureiro
  • 957
  • 1
  • 12
  • 34
  • It doesn't work with `sort` which ignores the difference of case-insenitives words. – Oo'- Oct 21 '17 at 18:00
0

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>.";
  }
 }

?>
Sharanya Dutta
  • 3,981
  • 2
  • 17
  • 27
  • But `in_array` doesn't ignore case-insensitive string comparison. Only `sort` ignores. – Oo'- Oct 21 '17 at 18:13
0

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);
0

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.";
  }
}
MatsLindh
  • 49,529
  • 4
  • 53
  • 84
  • I did a test with your codes... did you put `;` before the dictionary's bracket close? Is `;` need in the dictionary's bracket close? PHP 7.2 told there is a missed bracket. – Oo'- Oct 21 '17 at 23:40
  • Yes, there should be a `;` there. – MatsLindh Oct 22 '17 at 09:06