How can htmlentities() be used to convert quotation marks in a URL code for proper embedding in an iframe?

When embedding a URL in an iframe, any quotation marks within the URL code can cause issues with the HTML syntax. To solve this problem, you can use the htmlentities() function in PHP to convert the quotation marks to their HTML entity equivalents. This ensures that the URL code is properly formatted for embedding in an iframe without causing any syntax errors.

$url = 'https://www.example.com/?param1=value1&param2=value2" onclick="alert(\'XSS Attack\')';
$encoded_url = htmlentities($url, ENT_QUOTES);
echo '<iframe src="' . $encoded_url . '"></iframe>';