3

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
  • 1
    Please note that custom1 and custom2 are not valid HTML attributes. If you need custom attributes, use the HTML5's data-* attributes (data-custom1="custom" ...). Commented Aug 6, 2012 at 15:18

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I will give them both a try.
0

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) {
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.