How to Remove Script Tags from HTML Content: A Comprehensive Guide
While using HTML Purifier, you may encounter the need to specifically remove script tags without affecting inline formatting or other elements. To achieve this, consider the following approaches:
Using Regular Expressions:
Although not recommended for parsing HTML/XML, regular expressions can be used as a quick fix:
$html = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $html);
Using DOMDocument:
For a more reliable and secure approach, use DOMDocument:
$dom = new DOMDocument(); $dom->loadHTML($html); $script = $dom->getElementsByTagName('script'); foreach($script as $item) { $item->parentNode->removeChild($item); } $html = $dom->saveHTML();
Other Removal Methods:
In addition to these two options, consider other methods:
Remember to carefully consider the security implications of allowing or denying script tags based on the content and source of the HTML.
The above is the detailed content of How to Remove Script Tags from HTML Content Without Affecting Formatting?. For more information, please follow other related articles on the PHP Chinese website!