What are the potential pitfalls of using strip_tags() function in PHP for removing HTML elements?

The strip_tags() function in PHP removes HTML tags from a string, but it does not handle attributes or nested tags. This can lead to unexpected results if the input contains attributes or nested tags that are not properly sanitized. To properly remove all HTML elements and their attributes, it is recommended to use a more robust HTML parsing library like DOMDocument or a dedicated HTML sanitizer.

// Using DOMDocument to remove all HTML elements and their attributes
function strip_html_tags($html) {
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    return $dom->textContent;
}

// Example usage
$html = '<p>Hello <strong>world</strong>!</p>';
$cleaned_html = strip_html_tags($html);
echo $cleaned_html; // Output: Hello world!