PHP: DomElement->getAttribute

php domelement to string
php domelement get all attributes
php domelement get html
php domelement get text
php domelement innerhtml
php xpath
php domdocument get element by class
php domelement get child nodes

How can I take all the attribute of an element? Like on my example below I can only get one at a time, I want to pull out all of the anchor tag's attribute.

$dom = new DOMDocument();
@$dom->loadHTML(http://www.example.com);

$a = $dom->getElementsByTagName("a");
echo $a->getAttribute('href');

thanks!

"Inspired" by Simon's answer. I think you can cut out the getAttribute call, so here's a solution without it:

$attrs = array();
for ($i = 0; $i < $a->attributes->length; ++$i) {
  $node = $a->attributes->item($i);
  $attrs[$node->nodeName] = $node->nodeValue;
}
var_dump($attrs);

DOMElement::getAttribute - Manual, Description �. public DOMElement::getAttribute ( string $name ) : string. Gets the value of the attribute with name name for the current node. The DOMElement::setAttribute() function is an inbuilt function in PHP which is used to set an attribute with given name to the given value. If the attribute does not exist, it will be created. If the attribute does not exist, it will be created.

$length = $a->attributes->length;
$attrs = array();
for ($i = 0; $i < $length; ++$i) {
    $name = $a->attributes->item($i)->name;
    $value = $a->getAttribute($name);

    $attrs[$name] = $value;
}


print_r($attrs);

DOMElement::__construct - Manual, A namespace URI to create the element within a specific namespace. Examples �. Example #1 Creating a new DOMElement. <?php $dom� DOMElement::hasAttribute() - Checks to see if attribute exists DOMElement::setAttribute() - Adds new attribute DOMElement::removeAttribute() - Removes attribute

$a = $dom->getElementsByTagName("a");
foreach($a as $element)
{
   echo $element->getAttribute('href');
}

DOMDocument - Manual, The DOMDocument class �. (PHP 5, PHP 7). Introduction �. Represents an entire HTML or XML document; serves as the root of the� I'm struggling big time understanding how to use the DOMElement object in PHP. I found this code, but I'm not really sure it's applicable to me:

$html = $data['html'];
if(!empty($html)){
   $doc = new DOMDocument();
   $doc->loadHTML($html);
   $doc->saveHTML();
   $datadom = $doc->getElementsByTagName("input");
   foreach($datadom as $element)
   {
       $class =$class." ".$element->getAttribute('class');
   }
}

DOMDocument::getElementsByTagName - Manual, DOMDocument::getElementsByTagName. (PHP 5, PHP 7). DOMDocument:: getElementsByTagName — Searches for all elements with given local tag name � DOMElement::setIdAttribute (PHP 5, PHP 7) DOMElement::setIdAttribute — Declares the attribute specified by name to be of type ID. Description.

DOMDocument::createElement - Manual, (PHP 5, PHP 7). DOMDocument::createElement — Create new element node This function creates a new instance of class DOMElement. This node will not� This function returns a new instance of the class DOMNodeList of all descendant elements with a given tag name, in the order in which they are encountered in a preorder traversal of this element tree.

DOMElement::setAttribute - Manual, Raised if the node is readonly. Examples �. Example #1 Setting an attribute. <? php $doc =� I realised, that there is a different when using different libxml2 versions on Apache2. This code will fail with libxml2 version 2.6.23 and PHP version 5.2.6--->It works fine with libxml2 version 2.6.32 and PHP version 5.2.6-3ubuntu4.6->and finally it also works with libxml2 2.7.7 and PHP >= 5.3

DOMElement::getElementsByTagName - Manual, DOMElement::getElementsByTagName. (PHP 5, PHP 7). DOMElement:: getElementsByTagName — Gets elements by tagname� PHP | DOMElement getAttributeNode() Function The DOMElement::getAttributeNode() function is an inbuilt function in PHP which is used to get the attribute node with name, for the current element. Syntax:

Comments
  • If I read the question right, he wants all the element's attributes. Not one attrbiute from all elements.