<?php
/**
* Decodes HTML input for routine automation.
*
* @param string $html_input The HTML string to decode.
* @return string|string[] Returns the decoded HTML string on success, or an array of error messages on failure.
*/
function decodeHtml(string $html_input): string|string[]
{
// Use strip_tags to remove HTML tags from the input.
$decoded_html = strip_tags($html_input);
// Check if the input was successfully decoded.
if (empty($decoded_html)) {
return ['Error: No content found after decoding.'];
}
return $decoded_html;
}
// Example usage (for testing):
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$html_input = $_POST["html_input"] ?? '';
$result = decodeHtml($html_input);
if (is_array($result)) {
// Display error messages
echo "<div style='color:red;'>";
foreach ($result as $error) {
echo htmlspecialchars($error) . "<br>"; //Escape for safety
}
echo "</div>";
} else {
// Display decoded HTML
echo "<div style='margin-top: 20px;'>";
echo "<pre>" . htmlspecialchars($result) . "</pre>"; //Escape for safety and preserve formatting
echo "</div>";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>HTML Decoder</title>
</head>
<body>
<form method="post">
<label for="html_input">Enter HTML:</label><br>
<textarea id="html_input" name="html_input" rows="10" cols="50"></textarea><br><br>
<input type="submit" value="Decode">
</form>
</body>
</html>
Add your comment