<?php
// powered by knuepower.de
$stats = [];
$text_input = $_POST['text_input'] ?? '';

if (isset($_POST['analyze_text'])) {
    $text = trim($text_input);
    $stats['chars_with_spaces'] = mb_strlen($text);
    $stats['chars_without_spaces'] = mb_strlen(str_replace(' ', '', $text));
    $stats['words'] = str_word_count($text, 0);
    $stats['lines'] = substr_count($text, "\n") + 1;
}
?>
<div class="card mb-4" id="text-counter">
  <div class="card-header fw-bold">✏️ Zeichen- & Wortzähler</div>
  <div class="card-body">
    <form method="post" action="#text-counter">
      <textarea name="text_input" class="form-control mb-3" rows="4" placeholder="Text hier eingeben..." required><?= htmlspecialchars($text_input) ?></textarea>
      <button type="submit" name="analyze_text" class="btn btn-primary">Analysieren</button>
    </form>
    <?php if (!empty($stats)): ?>
      <div class="mt-4">
        <ul class="list-group">
          <li class="list-group-item d-flex justify-content-between"><span>Zeichen (mit Leerzeichen)</span><strong><?= $stats['chars_with_spaces'] ?></strong></li>
          <li class="list-group-item d-flex justify-content-between"><span>Zeichen (ohne Leerzeichen)</span><strong><?= $stats['chars_without_spaces'] ?></strong></li>
          <li class="list-group-item d-flex justify-content-between"><span>Wörter</span><strong><?= $stats['words'] ?></strong></li>
          <li class="list-group-item d-flex justify-content-between"><span>Zeilen</span><strong><?= $stats['lines'] ?></strong></li>
        </ul>
      </div>
    <?php endif; ?>
  </div>
</div>
