What PHP functions should be used to ensure the security and integrity of user-generated content on a website?

To ensure the security and integrity of user-generated content on a website, it is important to sanitize and validate the data before storing or displaying it. This helps prevent SQL injection, cross-site scripting (XSS) attacks, and other security vulnerabilities. PHP provides several functions that can be used for this purpose, such as htmlentities() for encoding HTML entities, mysqli_real_escape_string() for escaping special characters in SQL queries, and filter_var() for validating input data.

// Example of sanitizing user input using htmlentities()
$userInput = "<script>alert('XSS attack')</script>";
$sanitizedInput = htmlentities($userInput);
echo $sanitizedInput;