Are there any PHP libraries or functions specifically designed to handle user-generated content and prevent HTML injection?

User-generated content can pose a security risk as it may contain HTML or script tags that can lead to HTML injection attacks. To prevent this, PHP offers functions like htmlspecialchars() which converts special characters to HTML entities, escaping them and rendering them harmless. By using this function to sanitize user input before displaying it on a webpage, you can prevent HTML injection attacks.

$userInput = "<script>alert('Hello!');</script>";
$sanitizedInput = htmlspecialchars($userInput);
echo $sanitizedInput;