Remove HTML tags from a string except p, b, i and br in PHP

2 months ago  |  50 views
$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']);

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.