0

How can I read tag attributes and classes with DOMDocument, DOMNode and the rest of the family?

And, intended to be working with HTML not XML.

Maybe any useful extensions/modules to be used for this?

Thanks in advance!

tomsseisums
  • 13,168
  • 19
  • 83
  • 145
  • (reference) [Best methods to parse HTML with PHP](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html-with-php) Question specific: [DomNode::$attributes](http://www.php.net/manual/class.domnode.php#domnode.props.attributes) – Yoshi Jul 28 '11 at 09:23

2 Answers2

3

Actually this is all in the PHP Manual, so I give some hints:

How can I read tag attributes and classes with DOMDocument, DOMNode and the rest of the family?

These objects have methods available to access attributes, like getAttribute.

And, intended to be working with HTML not XML.

Use DomDocument->loadHTML() to load HTML, this makes it very similar to XML handling.

Maybe any useful extensions/modules to be used for this?

It's all in the Dom Extension, normally you can do pretty everything with it and no additions are necessary. XPath is part of the Dom Extension as well. However there are libraries like QueryPath or FluentDom that might encapsulate a bit more what you're looking for, e.g. for classes.

hakre
  • 193,403
  • 52
  • 435
  • 836
3

DOMNode [docs] has an attributes property [docs].

Note that only DOMElements [docs] can actually have attributes.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143