How can htmlspecialchars() be utilized to ensure secure data handling when integrating Fancybox with PHP-generated content?

When integrating Fancybox with PHP-generated content, it is crucial to sanitize the data to prevent cross-site scripting (XSS) attacks. One way to do this is by using the htmlspecialchars() function in PHP to encode special characters in the content before displaying it in the Fancybox modal. This ensures that any potentially malicious scripts are rendered harmless.

<?php
// Assuming $content is the PHP-generated content to be displayed in Fancybox
$sanitized_content = htmlspecialchars($content, ENT_QUOTES, 'UTF-8');
echo '<a href="#" data-fancybox data-src="' . $sanitized_content . '">Open Fancybox</a>';
?>