Remove HTML tags from a string except p, b, i and br in PHP
Question:
How to remove HTML tags from a string except p, b, i and br in PHP? Answer:
$text = '<div class="test"><p>Clean <a href="#">text</a><br><b>Bold</b> text</p></div>';
$cleanText = strip_tags($text, ['p','b','i','br']);
Description:
Removing all HTML tags from a string in PHP is a quite simple task using the built-in strip_tags
function. However, in some cases, you want to allow some elements. Fortunately, the strip_tags function allows this as well. As a second parameter, you can list the allowed tags as an array.
Note: This function should not be used to try to prevent XSS attacks. It does not modify any attributes on the tags that you allow.
Reference:
PHP strip_tags reference
Share "How to remove HTML tags from a string except p, b, i and br in PHP?"
Related snippets:
Tags:
remove html tags except, allow tags for strip_tags Technical term:
Remove HTML tags from a string except p, b, i and br in PHP