$html = ''; $document = new DOMDocument(); $document->loadHTML($html); $element = $document->getElementsByTagName('div')->item(0); if ($element->hasAttribute('class')) { echo 'The element has a class attribute'; } else { echo 'The element does not have a class attribute'; }
$html = ''; $document = new DOMDocument(); $document->loadHTML($html); $element = $document->getElementsByTagName('div')->item(0); if ($element->hasAttribute('data-my-attribute')) { echo 'The element has a custom attribute'; } else { echo 'The element does not have a custom attribute'; }In this example, we get the first `div` element in the DOM, and check if it has a custom attribute called `data-my-attribute`. Since it does, the output would be "The element has a custom attribute". The package library used in these examples is the PHP DOM extension, which provides a way to work with HTML and XML documents as a tree of DOM nodes.