What are some potential pitfalls of allowing HTML in user-generated content in PHP applications?

Allowing HTML in user-generated content can lead to security vulnerabilities such as cross-site scripting (XSS) attacks, where malicious scripts can be injected into the webpage and executed in the context of other users. To prevent this, it is important to sanitize and validate user input before displaying it on the webpage.

// Sanitize and validate user input before displaying it
$userInput = '<script>alert("XSS attack")</script>';
$cleanUserInput = htmlspecialchars($userInput, ENT_QUOTES, 'UTF-8');
echo $cleanUserInput;