What are the best practices for securely extracting attributes from HTML tags in PHP?

When extracting attributes from HTML tags in PHP, it is important to sanitize the input to prevent XSS attacks and ensure that only the desired attributes are extracted. One way to securely extract attributes is by using the DOMDocument class in PHP, which provides methods for parsing and manipulating HTML documents.

// Example code to securely extract attributes from HTML tags in PHP using DOMDocument

$html = '<a href="https://example.com" title="Example">Click here</a>';

$dom = new DOMDocument();
$dom->loadHTML($html);

$links = $dom->getElementsByTagName('a');

foreach ($links as $link) {
    $href = $link->getAttribute('href');
    $title = $link->getAttribute('title');
    
    // Sanitize and use the extracted attributes
    $sanitizedHref = filter_var($href, FILTER_SANITIZE_URL);
    $sanitizedTitle = filter_var($title, FILTER_SANITIZE_STRING);

    echo "Href: $sanitizedHref, Title: $sanitizedTitle";
}