<?php
// scripts/regex_tester.php
// Ejecuta en navegador y pega texto + regex para probar.
if ($_SERVER['REQUEST_METHOD']==='POST') {
  $text = $_POST['text'] ?? '';
  $regex = $_POST['regex'] ?? '/\b(\d{6})\b/';
  $ok = @preg_match_all($regex, $text, $m);
}
?>
<!doctype html>
<html lang="es"><head><meta charset="utf-8"><title>Regex Tester</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"></head>
<body class="p-3">
<h3>Probar Regex</h3>
<form method="post" class="mb-3">
  <div class="mb-2">
    <label class="form-label">Regex (PHP)</label>
    <input class="form-control" name="regex" value="<?= htmlspecialchars($_POST['regex'] ?? '/\\b(\\d{6})\\b/') ?>">
    <div class="form-text">Ejemplo: <code>/\b(\d{6})\b/</code> — 6 dígitos.</div>
  </div>
  <div class="mb-2">
    <label class="form-label">Texto (asunto + cuerpo)</label>
    <textarea class="form-control" name="text" rows="8"><?= htmlspecialchars($_POST['text'] ?? '') ?></textarea>
  </div>
  <button class="btn btn-primary">Probar</button>
</form>
<?php if (isset($ok)): ?>
  <h5>Resultados</h5>
  <pre><?php
    if ($ok === false) { echo "Regex inválida."; }
    else {
      $codes = $m[1] ?? [];
      // Normaliza códigos con espacios
      $codes = array_map(fn($c)=>preg_replace('/\s+/', '', $c), $codes);
      echo "Encontrados: ".count($codes)."\n";
      foreach ($codes as $i=>$c) echo sprintf("%02d) %s\n", $i+1, $c);
    }
  ?></pre>
<?php endif; ?>
</body></html>
