2

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).

1

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.