What are the best practices for formatting and displaying HTML content retrieved from a file in PHP?

When displaying HTML content retrieved from a file in PHP, it's important to properly format and sanitize the content to prevent security vulnerabilities like cross-site scripting (XSS) attacks. One way to do this is by using the htmlentities function to encode special characters in the HTML content before displaying it on the page.

<?php
// Retrieve HTML content from a file
$htmlContent = file_get_contents('file.html');

// Sanitize and format the HTML content
$sanitizedContent = htmlentities($htmlContent);

// Display the sanitized HTML content
echo $sanitizedContent;
?>