I need a script that opens a given URL and reads all the CSS associated with that page. Like the [URL=http://jigsaw.w3.org/css-validator/]W3C’s CSS validator[/URL], without actually validating anything. I want to pass that CSS information on to another application for parsing. Surely this script already exists? I can’t imagine I would need to write it from scratch.
I [URL=http://www.webdeveloper.com/forum/showthread.php?t=126929]posted about this earlier[/URL] in the JavaScript forum, but I’m thinking that server-side is a much better way to do what I’m looking for. Thanks for your help! ?
@bokehNov 03.2006 — #What about CSS embeded in the HTML elements? It would have no context if extracted from the page. Other than that it is easy. Open the page and read the CSS between style elements. Follow all link elements that lead to stylesheets. Follow all @import() within inline or external stylesheets and download those stylesheets. Follow all @import() within any descendant stylesheets.
@badcoyoteauthorNov 03.2006 — #Thank you. I already had this logic figured out, but I appreciate your help. I can ignore inline styles, they pose a number of problems beyond just reading them. For the rest, this is pretty sensible.
I guess my problem is that this is a new sort of PHP development for me. Generally I use it to generate dynamic pages, process form data, etc. I don't really know where to begin parsing (X)HTML documents to extract what I'm looking for. Maybe I shouldn't be posting here at this point, this forum seems to be more for devs who already have code written and are just trying to debug. But if you have any recommended guides on learning to parse markup with PHP, I'd love some advice.
@NogDogNov 03.2006 — #You're probably looking at using regular expressions, such as with the preg_match_all() function, to extract the links and text of interest. There have been more than a few threads here on extracting text between tags or attributes from within tags, so you might want to try searching here to get some idea of what's involved. And of course there's lots of info in the online PHP manual: http://www.php.net/manual/en/ref.pcre.php
@bokehNov 03.2006 — #this forum seems to be more for devs who already have code written and are just trying to debug[/QUOTE]This forum is for everyone and every level. If the answers don't suit your level just say so.
Start by fetching the file:[code=php]$content = file_get_contents('http://domain.com/file.htm'); #get CSS $css = null; if(preg_match('/<styleb[^>]*>((?:(?!</style>).)+)</style>/is', $content, $match)) { $css = match[1]; }
@badcoyoteauthorNov 03.2006 — #Excellent help, my thanks to both of you! Sorry if I came across rude or inept, there's always an awkwardness when one is new in a forum. I do have some direction for this project now, and I really appreciate it. You guys are great. ?