Reputation: 129
After some searching I have come to the conclusion that it is not possible to retrieve CSS styles using the PHP DOM parser
. Can anyone confirm that?
An element can e.g. be identifyed by its ID
. If that element then has a style attribute (an element style style="abc"
), this attribute's value can be retrieved too. But if the element's style is defined by an id
from a CSS selector
, PHP seems not to have any direct access to the styles.
The only workaround I see is parsing all linked CSS files and checking if there are any styles assigned to that ID
. Is that right?
Upvotes: 4
Views: 4727
Reputation: 145472
Pretty much correct. The DOM parser does not load external dependencies, and in particular does not care about CSS. You have to load them separately and process distinctly from the DOM.
It seems you could use a CSS parser for that. See Parsing CSS by regex for some pointers. http://pear.php.net/package/HTML_CSS might be a usable choice for the task. But you will still have to iterate over existing CSS definitions to detect the ID you're interested in.
https://github.com/sabberworm/PHP-CSS-Parser also looks useful. It seemingly supports css selectors for querying style settings.
Upvotes: 1