I can get the attribute of the tag tragetting ID by:
$tag_id = $tag->getAttribute('id');
But I want to get all attributes, not just id
. How can I do this? I tried getAttribute("*")
but it did not work (obviously).
I can get the attribute of the tag tragetting ID by:
$tag_id = $tag->getAttribute('id');
But I want to get all attributes, not just id
. How can I do this? I tried getAttribute("*")
but it did not work (obviously).
You can use the public $attributes
property from the DOMNode element that the DOMElement inherits from. See also here in the documentation: DOMNode::$attributes
Code example:
$document = <<<DOCUMENT
<div id='one' style='width: 10px'></div>
DOCUMENT;
$document = DOMDocument::loadXML($document);
$element = $document->getElementsByTagName('div')->item(0);
$attributes = $element->attributes;
for ($i = 0; $i < $attributes->length; $i++) {
$item = $attributes->item($i);
echo 'There is an attribute called: "' . $item->nodeName . '" with value: ' . $item->nodeValue . PHP_EOL;
}
See also the eval.in that I created: https://eval.in/494934