Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Php Css Parser

Php Css Parser Laravel Package

sabberworm/php-css-parser

A PHP library that parses CSS files into a manipulable data structure, allowing programmatic editing of rules and declarations before outputting optimized CSS. Supports custom charsets, strict parsing and optional multi-byte functions. Install via Composer.

View on GitHub
Deep Wiki
Context7

PHP CSS Parser is a PHP library that parses CSS files into a manipulatable data structure, letting you extract, modify, and re-output stylesheets as optimized CSS. It handles real-world CSS including @charset declarations and legacy IE filter rules, with configurable settings for charset, strict parsing, and performance.

  • Parse CSS strings or entire files via a simple Parser API
  • Manipulate the resulting document tree before output
  • Strict mode to throw on invalid or unknown constructs
  • Configurable default charset (UTF-8 by default)
  • Optional multibyte-free parsing for faster performance
Frequently asked questions about Php Css Parser
How do I install sabberworm/php-css-parser in a Laravel project?
Run composer require sabberworm/php-css-parser from your Laravel project root. The package has no Laravel service provider requirement, so you can use the classes directly or wrap them in your own singleton binding or facade. Upgrading to ^9.5 requires no code changes to the Parser, Settings, or OutputFormat APIs.
Can I use PHP CSS Parser for dynamic theming and runtime CSS transformation in Laravel?
Yes. The parser extracts a stylesheet into a manipulable data structure, so you can inject user-specific rules, swap colors for dark mode, or scope selectors at runtime. Bind the parser as a singleton in a service provider and trigger transformations from a view.rendered or assets.processed listener. For repeated requests, cache the transformed output rather than reparsing on every hit.
Which PHP and Laravel versions does sabberworm/php-css-parser support?
Version 9.5.0 extends the supported runtime ceiling to PHP 8.6, so teams tracking the newest PHP minor no longer need to pin the parser. The package is framework-agnostic and works with any Laravel release that runs on a supported PHP version. Adding PHP 8.6 to your CI matrix only makes sense once Laravel, PHPUnit, and your static analysis tooling support it too.
How do I parse a CSS file with PHP CSS Parser instead of a string?
Create a new \Sabberworm\CSS\Parser instance with the file contents and call parse() to get the CSS document object. For large stylesheets, stream the file with fopen() and fread() rather than file_get_contents() to keep memory usage down. The resulting document can then be manipulated and rendered with render().
How do I output minified or optimized CSS from the parser?
Pass an OutputFormat instance to the document's render() method, using OutputFormat::createCompact() for production output. Predefine your OutputFormat objects once rather than rebuilding them per request. Compact output strips unnecessary whitespace, which is typically what you want for build-time or cached runtime generation.
What is the difference between strict and non-strict parsing in PHP CSS Parser?
By default the parser ignores unknown or invalid constructs and keeps going, which is safer for real-world stylesheets. Calling Settings::create()->beStrict() makes it throw an exception on invalid constructs instead. Note that strict mode also disables the workaround for the legacy unquoted IE filter rule, so only enable it when you are deliberately validating CSS syntax.
How do I make PHP CSS Parser faster by disabling multibyte support?
Use Settings::create()->withMultibyteSupport(false) so the parser uses regular string functions instead of mb_* calls. This is fine for most input, including UTF-8 files, since multibyte characters usually sit inside string literals. Avoid it for untrusted input, because that path is not thoroughly covered by the test suite.
What changed in version 9.5.0 and will upgrading break my Laravel app?
There are no public API changes to Parser, Settings, or OutputFormat, so existing code keeps working. The release fixes @layer statement parsing, which previously consumed input until the next brace, so any cached or committed transform artifacts produced under 9.4 should be regenerated. It also drops thecodingmachine/safe from the dependency graph, so run composer why thecodingmachine/safe and declare it explicitly if your own code calls Safe\* functions.
How do I test CSS transformations built with PHP CSS Parser in PHPUnit?
Write golden-file tests that feed a fixture stylesheet through the parser and compare the rendered output against an expected string. This catches output drift across parser upgrades, including the @layer fix in 9.5.0. Also include legacy CSS fixtures such as IE filter: alpha() hacks to confirm backward compatibility, and assert that your transformation rules produce the expected selectors and declarations.
Is PHP CSS Parser a replacement for a CSS minifier or Laravel Mix/Vite?
Not exactly. It is a low-level CSS manipulation layer for parsing, rewriting, and re-rendering stylesheets as data, while Mix and Vite focus on bundling and asset pipelines. Many teams use both: Vite for build-time bundling and the parser for rule-based transformations such as removing unused properties or injecting scoped selectors. If you only need whitespace minification, a dedicated minifier may be simpler.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
elmogy/larafast
xeviant/laravel-paystack
gorilladash/laravel-website-sdk
martialbe/flysystem-oss
chondal/3312client
jeroen-g/flickr
opscale-co/nova-genbi
itsjustvita/laravel-retention
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log
gregsphotography/site-builder-components
io-digital/clickatell
snowsoft/laravel-admin
oxygen/auth
relicsoftware/barakah-payment-gateway
tobya/work-with-composer
equipc/laravel-email-validator
equipc/laravel-email-database-log
adedsec/nova-translated-labels