How can PHP functions like htmlspecialchars() help prevent security vulnerabilities when outputting user-generated HTML content?
When outputting user-generated HTML content, it is important to sanitize the input to prevent cross-site scripting (XSS) attacks. PHP functions like htmlspecialchars() can help by converting special characters in the input to their HTML entities, preventing them from being interpreted as code by the browser.
$userInput = "<script>alert('XSS attack!');</script>";
$sanitizedInput = htmlspecialchars($userInput, ENT_QUOTES, 'UTF-8');
echo $sanitizedInput;