Remove HTML Tags Selectively in PHP
Question: How can I selectively remove all HTML tags from a string in PHP, except for a specified list of allowed tags?
Answer: To remove all HTML tags while preserving a specific set of allowed tags, utilize PHP's strip_tags function. The strip_tags function allows you to specify a string containing the tags you want to retain.
Implementation:
<code class="php">$content = '<p>This is some HTML code with <br> and <a> tags.</a></p>'; echo strip_tags($content, '<p><br><a>');</code>
In this example, the strip_tags function will remove all HTML tags except for
,
, and , preserving the content within these tags. The output will be:
<p>This is some HTML code with and tags.</a></p>
The above is the detailed content of How to Selectively Remove HTML Tags in PHP?. For more information, please follow other related articles on the PHP Chinese website!