Are there any best practices or tutorials available for efficiently filtering attributes in HTML tags using regular expressions in PHP?

When filtering attributes in HTML tags using regular expressions in PHP, it is important to use a reliable regular expression pattern that accurately captures the attributes you want to filter. Best practices include using the preg_replace() function to remove unwanted attributes or using preg_match() to extract specific attributes. Tutorials and resources are available online to help you understand and implement regular expressions effectively for attribute filtering in HTML tags.

$html = '<div class="example" id="test" data-info="123">Content</div>';

// Remove all attributes except class and id
$filtered_html = preg_replace('/(<[^>]+) (?!(class|id)\b)[^>]*>/i', '$1>', $html);

echo $filtered_html;