问题
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