PHP: DomElement->getAttribute

前端 未结 4 571
失恋的感觉
失恋的感觉 2020-12-04 01:52

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.



        
相关标签:
4条回答
  • 2020-12-04 02:07

    "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);
    
    0 讨论(0)
  • 2020-12-04 02:13
    $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');
       }
    }
    
    0 讨论(0)
  • 2020-12-04 02:23
    $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);
    
    0 讨论(0)
  • 2020-12-04 02:29
    $a = $dom->getElementsByTagName("a");
    foreach($a as $element)
    {
       echo $element->getAttribute('href');
    }
    
    0 讨论(0)
提交回复
热议问题