1. <?php
  2. /**
  3. * Decodes HTML input for routine automation.
  4. *
  5. * @param string $html_input The HTML string to decode.
  6. * @return string|string[] Returns the decoded HTML string on success, or an array of error messages on failure.
  7. */
  8. function decodeHtml(string $html_input): string|string[]
  9. {
  10. // Use strip_tags to remove HTML tags from the input.
  11. $decoded_html = strip_tags($html_input);
  12. // Check if the input was successfully decoded.
  13. if (empty($decoded_html)) {
  14. return ['Error: No content found after decoding.'];
  15. }
  16. return $decoded_html;
  17. }
  18. // Example usage (for testing):
  19. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  20. $html_input = $_POST["html_input"] ?? '';
  21. $result = decodeHtml($html_input);
  22. if (is_array($result)) {
  23. // Display error messages
  24. echo "<div style='color:red;'>";
  25. foreach ($result as $error) {
  26. echo htmlspecialchars($error) . "<br>"; //Escape for safety
  27. }
  28. echo "</div>";
  29. } else {
  30. // Display decoded HTML
  31. echo "<div style='margin-top: 20px;'>";
  32. echo "<pre>" . htmlspecialchars($result) . "</pre>"; //Escape for safety and preserve formatting
  33. echo "</div>";
  34. }
  35. }
  36. ?>
  37. <!DOCTYPE html>
  38. <html>
  39. <head>
  40. <title>HTML Decoder</title>
  41. </head>
  42. <body>
  43. <form method="post">
  44. <label for="html_input">Enter HTML:</label><br>
  45. <textarea id="html_input" name="html_input" rows="10" cols="50"></textarea><br><br>
  46. <input type="submit" value="Decode">
  47. </form>
  48. </body>
  49. </html>

Add your comment