Are there any specific PHP functions or libraries that can help mitigate the risks associated with allowing user input in iframes?

When allowing user input in iframes, a common risk is the possibility of cross-site scripting (XSS) attacks. To mitigate this risk, you can use the `htmlspecialchars()` function in PHP to encode user input before displaying it in an iframe. This function will convert special characters into their HTML entities, preventing any malicious scripts from being executed.

<?php
$user_input = "<script>alert('XSS attack!');</script>";
$encoded_input = htmlspecialchars($user_input, ENT_QUOTES, 'UTF-8');
echo "<iframe srcdoc='$encoded_input'></iframe>";
?>