PHP CSS Parser

PHP CSS Parser

1 January 1970

I’ve spent hours online looking for a CSS parser to use with PHP but all searches came up empty, half-assed or required me to register with some strange sites. Frustrated, I decided to write my own parser which is pretty much working but very incomplete. I don’t know whether this will be another write-once, update-never thing like much other PHP code on the web is but one feature is definitely coming: The ability to generate output from the parsed structure (which can be programmatically modified after parsing – or indeed have been created from scratch in PHP).

Some other features are planned as well but if no-one else is willing to work on them, them ever getting implemented will depend on whether I myself need them at some point.

Without much further ado; here it is:

Update: Changed from a gist into a full-blown GitHub repository.


Allison Allain at 08/23/10:

I love you app, and am an iPhone programmer. Is there any way that I can be of service?

Elod Molnar at 10/20/10:

Hello! At github, I don't know how, you didn't returned $aColor at parseColorValue() Best regards, Előd

Raphael at 11/18/10:

Thanks for the hint, Előd. It’s fixed now and I’ll write the unit tests for it soon.

CSJakharia at 02/06/11:

It did not worked on jquery-ui.css. Is there any forum or any such thing where I can post the stuff? jQueryUi.css is there at Error Fatal error: Uncaught exception 'Exception' with message 'Identifier expected, got (Opac' in \sabberworm-PHP-CSS-Parser-c779825\CSSParser.php:98 Stack trace: #0 \sabberworm-PHP-CSS-Parser-c779825\CSSParser.php(237): CSSParser->parseIdentifier() #1 \sabberworm-PHP-CSS-Parser-c779825\CSSParser.php(206): CSSParser->parseValue() #2 \sabberworm-PHP-CSS-Parser-c779825\CSSParser.php(194): CSSParser->parseRule() #3 \sabberworm-PHP-CSS-Parser-c779825\CSSParser.php(188): CSSParser->parseRuleSet(Object(CSSSelector)) #4 \sabberworm-PHP-CSS-Parser-c779825\CSSParser.php(50): CSSParser->parseSelector() #5 in \sabberworm-PHP-CSS-Parser-c779825\CSSParser.php on line 98
Thanks in advance CSJakharia

Roger at 03/16/11:

What license is this code under? Would you mind emailing me?

forex reviews at 04/10/11:

This really solved my problem, thank you!

Mark at 10/30/11:

Thanks for this parser. I was hoping if you could help me. I want to open my main CCS file (main.css) and have it read all of the imported files? @import url('../styles/global/reset.css'); @import url('/styles/themes/helloWorld/structure.css'); @import url('/styles/themes/helloWorld/presentation.css'); The documentation says to: use getContents(), then iterate over that collection and use instanceof to check whether you’re dealing with another CSSImport This is what I have: $oCssParser = new CSSParser(file_get_contents('main.css')); $oCssDocument = $oCssParser->parse(); getContents($oCssDocument); Any chance you would be willing to provide some more assistance? THX

Raphael at 11/07/11:

Hi Mark, have a look at ju1ius’ work. This fork has added the ability to resolve @import statements. Sincerely, Raphael

Andrew at 05/11/12:


It's breaking on one of the CSS files from facebooks unsupported browser page, it goes into an infinite loop on the private parseList() method.

Any ideas?

Praise at 08/11/12:

Is a good thing ! Thanks :-)

Comments are moderated.

Add a comment