What are the best practices for handling JavaScript execution when including external content in PHP using file_get_contents()?

When including external content in PHP using file_get_contents(), it's important to be cautious about executing JavaScript code from the external source. To prevent any potential security risks, you can sanitize the retrieved content by removing any <script> tags or potentially harmful JavaScript code before outputting it on your page.

$url = &#039;https://www.example.com/external-content&#039;;
$content = file_get_contents($url);

// Remove any &lt;script&gt; tags or potentially harmful JavaScript code
$content = preg_replace(&#039;/&lt;script\b[^&gt;]*&gt;(.*?)&lt;\/script&gt;/is&#039;, &#039;&#039;, $content);

echo $content;