Use PHP DOM to access style properties.

After some searching, I came to the conclusion that it is not possible to extract CSS styles using the PHP DOM parser . Can anyone confirm this?

An element may, for example, indicate its ID . If this element has a style attribute (element style="abc" ), this attribute value can also be obtained. But if the style of an element is determined by an ID from a CSS selector , PHP does not seem to have direct access to the styles.

The only workaround I see is to parse all the related CSS files and check for the styles assigned to this ID . Is it correct?

+4
source share
1 answer

Pretty true. The DOM parser does not load external dependencies and, in particular, does not care about CSS. You must load them separately and process distinctly from the DOM.

It seems you can use a CSS parser for this. See CSS Regular Expression Analysis for some pointers. http://pear.php.net/package/HTML_CSS can be a useful option for a task. But you still have to iterate over existing CSS definitions to find the identifier you are interested in.

https://github.com/sabberworm/PHP-CSS-Parser also looks useful. It seems to support a css selector for requesting style settings.

+1
source

All Articles