Removing Script Tags from HTML Content Question: How can I selectively remove only tags from HTML content without affecting other formatting?</p> <p><strong>Answer:</strong></p> <p><strong>Using Regular Expressions (Regex)</strong></p> <p>While not recommended for parsing HTML, a simple regex can be used to remove <script> tags:</p> <pre>$html = preg_replace('#<script(.*?)>(.*?)#is', '', $html); Using DOMDocument: A more reliable and safer approach is to use the DOMDocument class: $dom = new DOMDocument(); $dom->loadHTML($html); $script = $dom->getElementsByTagName('script'); $remove = []; foreach($script as $item) { $remove[] = $item; } foreach ($remove as $item) { $item->parentNode->removeChild($item); } $html = $dom->saveHTML(); Additional Options: Using PHP's Native strip_tags() Function While it doesn't selectively remove tags, it can be used to remove all HTML tags:</p> <pre>$html = strip_tags($html, '<p><a><b>');</pre> <p><strong>Using HTML Purifier</strong></p> <p>If you wish to perform comprehensive HTML parsing and security measures, consider using the HTML Purifier library:</p> <pre>$config = HTMLPurifier_Config::createDefault(); $config->set('Core.RemoveScript', true); $purifier = new HTMLPurifier($config); $html = $purifier->purify($html);</pre>