Get CSS style from PHP

后端 未结 3 1243
盖世英雄少女心
盖世英雄少女心 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:25

    As Martin says, in doing this you're almost writing a browser in PHP - it's a big ask! As with any big project, the key is to break it down into more manageable steps (although some of these aren't exactly straightforward).

    You'll need to:

    • work out which (if any) external css files are linked to
    • (to echo Gumbo): find (or develop) a way of reading and interpreting the external css, in-page css and inline css
    • work out what styles apply to each element (including styles applied by .class, #id and element type), and the parents of each element - including which css rules override which other css rules, etc.

    I wouldn't say it's impossible, as things like MPDF do almost the same thing (and may provide a good starting point) but I don't think there's a neat quick-fix.

提交回复
热议问题