Simple HTML DOM getting all attributes from a tag

前端 未结 4 1749
攒了一身酷
攒了一身酷 2020-12-15 13:18

Sort of a two part question but maybe one answers the other. I\'m trying to get a piece of information out of an

4条回答
  •  误落风尘
    2020-12-15 13:42

    I know this question is old, but the OP asked how they could get all the attributes in one statement. I just did this for a project I'm working on.

    You can get all the attributes for an element with the getAllAttributes() method. The results are automatically stored in an array property called attr.

    In the example below I am grabbing all links but you can use this with whatever you want. NOTE: This also works with data- attributes. So if there is an attribute called data-url it will be accessible with $e->attr['data-url'] after you run the getAllAttributes method.

    In your case the attributes your looking for will be $e->attr['data1'] and $e->attr['data2']. Hope this helps someone if not the OP.

    Get all Attributes

    $html = file_get_html('somefile.html');
    foreach ($html->find('a') as $e) {   //used a tag here, but use whatever you want
        $e->getAllAttributes();
    
        //testing that it worked
        print_r($e->attr);
    }
    

提交回复
热议问题