<?php
error_reporting(E_ALL);
include '../includes/header.php';
echo "<h1>Kyle's Mad Lib Gen</h1>";
if (isset($_POST['submit'])) {
$name = ucfirst(strtolower(trim($_POST['name'])));
$emotion = strtolower(trim($_POST['emotion']));
$verb = strtolower(trim($_POST['verb']));
$adjective = strtolower(trim($_POST['adjective']));
$animal = strtolower(trim($_POST['animal']));
$number = trim($_POST['number']);
$food = strtolower(trim($_POST['food']));
$story = file_get_contents(__DIR__ . '/madlib.txt');
if ($story === false) {
echo "<p>You screwed something up.</p>";
} else {
$story = str_replace("{name}", $name, $story);
$story = str_replace("{emotion}", $emotion, $story);
$story = str_replace("{verb}", $verb, $story);
$story = str_replace("{adjective}", $adjective, $story);
$story = str_replace("{animal}", $animal, $story);
$story = str_replace("{number}", $number, $story);
$story = str_replace("{food}", $food, $story);
echo "<h2>Completed lib Mad</h2>";
echo "<p>" . nl2br(htmlspecialchars($story)) . "</p>";
echo "<hr>";
echo "<h2>String Ops</h2>";
echo "Name in uppercase: "
. htmlspecialchars(strtoupper($name)) . "<br>";
echo "Animal in lowercase: "
. htmlspecialchars(strtolower($animal)) . "<br>";
echo "Name reversed: "
. htmlspecialchars(strrev($name)) . "<br>";
echo "Total characters: "
. strlen($story) . "<br>";
echo "Total words: "
. str_word_count($story) . "<br>";
$words = explode(" ", $story);
echo "First word: "
. htmlspecialchars($words[0]) . "<br>";
echo "<hr>";
}
}
echo "<h2>Fill in the Blanks</h2>";
echo "<form method='post'>";
echo "Your name:<br>";
echo "<input type='text' name='name' required><br><br>";
echo "An emotion:<br>";
echo "<input type='text' name='emotion' required><br><br>";
echo "A verb ending in -ing:<br>";
echo "<input type='text' name='verb' required><br><br>";
echo "An adjective:<br>";
echo "<input type='text' name='adjective' required><br><br>";
echo "An animal:<br>";
echo "<input type='text' name='animal' required><br><br>";
echo "A number:<br>";
echo "<input type='number' name='number' required><br><br>";
echo "A food:<br>";
echo "<input type='text' name='food' required><br><br>";
echo "<input type='submit' name='submit' value='Create My Mad Lib'>";
include '../includes/footer.php';
?>