PHP CSS Parser

A pure-PHP parser that turns CSS files into an editable object tree and back into optimized stylesheets.

Library
Composer
vv9.4.0
1,825stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
73/100Good
Development Activity84
Maintenance56
Community64
Maturity60
Momentum28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
82/100Excellent
Architecture82
Code Quality85
Innovation80
Learning Curve82

PHP CSS Parser (the sabberworm/php-css-parser package) reads CSS text into a structured, navigable object model made up of documents, rule sets, declaration blocks and value objects. Once parsed, you can inspect, add, remove or rewrite any selector, rule or value programmatically and render the result back to a compact or pretty-printed stylesheet.

It is one of the most widely used CSS tooling libraries in the PHP ecosystem, powering email inliners, asset optimizers and CMS theming features. The parser is configurable through a settings object that controls charset handling, strict versus lenient parsing and multibyte function usage.

What You Get

  • A full CSS-to-object-tree parser covering at-rules, media queries, selectors, declarations and values.
  • A mutable object model for adding, removing and rewriting rules, then rendering back to CSS.
  • A settings object for charset, strict/lenient parsing and multibyte handling.
  • Optimized and pretty-print output formatters for compact or human-readable CSS.

Common Use Cases

  • Inlining CSS into HTML email templates for maximum client compatibility.
  • Post-processing or minifying stylesheets in a build or asset pipeline.
  • Rewriting or namespacing selectors when embedding third-party widgets.

Under The Hood

Architecture

The parser is organized around a recursive-descent tokenizer (ParserState) that walks the CSS string and builds a CSSDocument object graph under src/, with node types for rule sets, AtRule variants, DeclarationBlock, selectors and Value/RuleValueList objects; rendering is delegated to OutputFormat objects so the same tree can emit compact or pretty CSS.

Tech Stack

Pure PHP with no runtime dependencies, supporting modern PHP versions; uses Composer for autoloading, PHPUnit for tests and PHPStan for static analysis, with optional mb_* multibyte string handling controlled via the Settings object.

Code Quality

The codebase is mature and well-tested with an extensive tests/ suite covering parsing and rendering edge cases, follows PSR autoloading and typed properties, and has been hardened over many years of Hacktoberfest and community contributions; the 9.x line reflects a significant modernization of type coverage.

API Design

The public API is compact and intuitive: construct a Parser, call parse(), traverse or mutate the returned document, and call render(). The Settings builder makes configuration discoverable, though deep tree manipulation requires familiarity with the node class hierarchy.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search