PHP Simple HTML DOM list of attributes

对着背影说爱祢 提交于 2019-12-07 14:43:58

问题


The simple_html_dom library is great for getting known attributes, but is there a way to get a list of all the attributes for an element?

For example, if I have:

<div id="test" custom1="custom" custom2="custom">

I can easily get the id:

$el = $html->find('div');
$id = $el->id;

But, is it possible to get custom1 and custom2 if they are not known ahead of time? Ideally, the solution would produce an array of the NVP's for all attributes (id, custom1, custom2).


回答1:


$el->attr is an associated array of tag=>value s




回答2:


You can use get_object_vars to get an associative array, and then loop over them.

$attrs = get_object_vars($el);

foreach($attrs as $key=>$value) {
}


来源:https://stackoverflow.com/questions/11830933/php-simple-html-dom-list-of-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!