Get CSS style from PHP

后端 未结 3 1241
盖世英雄少女心
盖世英雄少女心 2021-01-19 14:59

I am parsing pages using Simple DOM parser. It is neat, but I would like to get the applied css style for each element. Not only the inline styles, but every style it applie

3条回答
  •  清酒与你
    2021-01-19 15:31

    That's a pretty tall order. Consider this simple example:

          
    

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.

    What color is the link? Each of the 5 styles applies directly to the link element. Even when you only consider CSS2.1, you still have 3 styles to process.

    As Gumbo says, without a full CSS parser and interpreter, this cannot be solved. I haven't seen one written in PHP yet, although it should be theoretically possible to write one.

    (There are classes for CSS parsing, yes - see the answers to this question, but those would only tell you "for this file, you have these CSS declarations". The interpreter is the hardest part, and I'm not aware of a PHP one)

    Your best bet would be rendering the page in some webpage rendering engine (e.g. Gecko or Webkit) and querying the CSS properties. That, unfortunately, is far beyond the scope of a simple PHP class.

提交回复
热议问题