$xml = ""; $doc = new DOMDocument(); $doc->loadXML($xml); $element = $doc->getElementsByTagName("example")[0]; if ($element->hasAttributes()) { foreach ($element->attributes as $attribute) { echo $attribute->nodeName . " = " . $attribute->nodeValue . "\n"; } }
$html = "This code creates an HTML document with a div element that has a class attribute. The hasAttributes() method is called on the element, and if it returns true, a message is outputted indicating that the element has attributes. If it returns false, a different message is outputted. Package Library: The PHP DOMElement class is part of the built-in PHP DOM extension, which provides a set of classes for working with HTML and XML documents in PHP. No additional package or library is required."; $doc = new DOMDocument(); $doc->loadHTML($html); $element = $doc->getElementsByTagName("div")[0]; if ($element->hasAttributes()) { echo "This element has attributes"; } else { echo "This element does not have attributes"; }Hello World