从PHP获取CSS样式 [英] Get CSS style from PHP

查看:144
本文介绍了从PHP获取CSS样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Simple DOM解析器解析页面。它是整洁的,但我想要获得每个元素应用的CSS样式。不仅内联样式,而且适用于该元素的每种风格都是内联的,页内的或外部的。



有没有一个类?如果没有,你会怎么做?我不太在乎重写样式,级联或浏览器特定样式。拥有所有直接应用的样式就足够了。

解决方案

正如Martin所说,在这样做时,你几乎在PHP中编写一个浏览器 - 这是一个大问题!与任何大项目一样,关键是将其分解成更易于管理的步骤(尽管其中一些步骤不完全简单)。



您需要:




  • 将外部CSS文件链接到(如果有的话)

  • (回覆Gumbo ):找到(或开发)一种阅读和解释外部CSS的方式,页内css和inline css

  • 确定什么样式适用于每个元素(包括.class应用的样式,#id和元素类型),并且每个元素的父母 - 包括哪些css规则覆盖其他css规则等。



我不会说这是不可能的,像 MPDF 这几乎是一回事(可能提供一个好的起点),但我不认为有一个整洁的快速修复。


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 applies to that element, being it inline, in-page or external.

Is there a class that does that? If not, how would you do it? I do not really care about overriding styles, cascade, or browser specific styles. Having all the directly applied styles would suffice.

解决方案

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.

这篇关于从PHP获取CSS样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆