/    Sign up×
Community /Pin to ProfileBookmark

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! ?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@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.
Copy linkTweet thisAlerts:
@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.

Thanks again.
Copy linkTweet thisAlerts:
@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
Copy linkTweet thisAlerts:
@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];
}

[/code]

That will get you the inline stylesheet.
Copy linkTweet thisAlerts:
@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. ?
Copy linkTweet thisAlerts:
@bokehNov 04.2006 — Next you will need to extract the URLs from the link elements to get the external stylesheets.
×

Success!

Help @badcoyote spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...