<?php
$string = "the quick brown fox jumped over the lazy dog";
$email = "halu@aspit.dk";

$elever = ["jonas", "jonas", "sebastian", "danny", "kasper"];
$find = "sebastian";

$months = ["januar" => 31, "febuar" => 28, "marts" => 31, "april" => 30, "maj" => 31, "juni" => 30, "juli" => 31, "august" => 31, "september" => 30, "oktober" => 31, "november" => 30, "december" => 31];

$laerere = array(array("fornavn" => "Hanne", "efternavn" => "Lund", "fag" => "Visualisering"), array("fornavn" => "Jens", "efternavn" => "Clausen", "fag" => "Softwarekonstruktion"), array("fornavn" => "Ronni", "efternavn" => "Hansen", "fag" => "Teknik"), array("fornavn" => "Ulf", "efternavn" => "Skaaning", "fag" => "AspIT-Lab"));

function FindPos($haystack, $needle)
{
    for ($i = 0; $i < count($haystack); $i++) {
        if ($haystack[$i] === $needle) return $i;
    }
    return null;
}

function RandomPassword($length)
{
    $res = "";
    $passwordChars = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz";
    for ($i = 0; $i < $length; $i++) {
        $res .= $passwordChars[rand(0, strlen($passwordChars) - 1)];
    }
    return $res;
}

function isPalindrom($input)
{
    $handledInput = str_split(strtoupper($input));
    $res = "";
    for ($i = count($handledInput); $i >= 0; $i--) {
        $res .= $handledInput[$i];
    }
    return $res == implode($handledInput);
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body {
            display: flex;
            flex-direction: column;
            gap: 2rem;
        }

        .row,
        .col {
            display: flex;
            gap: 0.5rem;
        }

        .col {
            flex-direction: column;
        }
    </style>
    <title>Opgaver</title>
</head>

<body>
    <p><?= strtoupper($string) ?></p>
    <p><?= ucfirst($string) ?></p>
    <p><?= ucwords($string) ?></p>
    <p><?php
        $word = "fox";
        if (str_contains($string, $word)) echo $word . " kunne findes";
        else echo $word . " kunne ikke findes";
        ?>
        <?php
        $word = "cat";
        if (str_contains($string, $word)) echo $word . " kunne findes";
        else echo $word . " kunne ikke findes";
        ?></p>
    <p><?= explode("@", $email)[0]; ?></p>
    <p>
        <?= RandomPassword(70); ?>
    </p>
    <p>
        <?= isPalindrom("Ana") ? "Is palindrom" : "Not palindrom"; ?>
    </p>
    <p>
        <?php
        $res = FindPos($elever, $find);
        if ($res !== null) echo ucfirst($find) . " kunne findes i arrayet på plads $res";
        else echo ucfirst($find) . " findes ikke i arrayet"
        ?>
    </p>

    <p>
        <?php
        $res = array_search($find, $elever);
        if ($res !== false) echo ucfirst($find) . " kunne findes i arrayet på plads $res";
        else echo ucfirst($find) . " findes ikke i arrayet";
        ?>
    </p>

    <div class="row">
        <div class="col">
            <?php
            foreach ($months as $month => $day) {
                if ($day < 31) echo "<p>$month: $day dage</p>";
            }
            ?>
        </div>
        <div class="col">
            <?php
            foreach ($months as $month => $day) {
                if ($day >= 31) echo "<p>$month: $day dage</p>";
            }
            ?>
        </div>
    </div>

    <div class="col">
        <?php
        foreach ($laerere as $laere) {
            echo "<div>";
            foreach ($laere as $key => $value) {
                echo "<p>$key: $value<p>";
            }
            echo "</div>";
        }
        ?>
    </div>
</body>

</html>