Weave Code
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

Parse and manipulate CSS in PHP with a fast, flexible parser. Convert CSS into an object model, inspect and edit rules, selectors, and declarations, then render back to CSS. Useful for minifying, rewriting assets, or building CSS tooling.

View on GitHub
Deep Wiki
Context7

A Parser for CSS Files written in PHP. Allows extraction of CSS files into a data structure, manipulation of said structure and output as (optimized) CSS

Frequently asked questions about Php Css Parser
How do I integrate sabberworm/php-css-parser into Laravel for dynamic CSS transformations?
Use Laravel’s service container to bind the parser as a singleton in `AppServiceProvider`. Inject it into controllers or Blade directives for runtime CSS manipulation. For example, create a facade like `Css::transform($css, $rules)` to wrap the parser’s logic in a Laravel-friendly API.
Can this package minify CSS in a Laravel Mix/Vite pipeline?
Yes. Use the parser’s `OutputFormat::createCompact()` to minify CSS during build. Integrate it as a PostCSS plugin or a custom Laravel Mix transformer to process CSS files before minification. For Vite, use a custom plugin to pipe CSS through the parser.
Does sabberworm/php-css-parser support Laravel 9+ and PHP 8.0+?
Yes, the package requires PHP 8.0+ and is fully compatible with Laravel 9+. It supports modern CSS3+ features like `calc()`, `@supports`, and custom properties, but can also handle legacy CSS with lenient parsing settings.
How do I handle large CSS files (1MB+) in Laravel without memory issues?
Stream files using `fopen()` and `fread()` instead of `file_get_contents()` to avoid memory overload. For async processing, dispatch the parsing task to Laravel Queues or use a worker service to handle large files in chunks.
What’s the best way to cache parsed CSS in Laravel to avoid reprocessing?
Cache the parsed CSS object using Laravel’s cache system (e.g., Redis or file cache). Store the parsed structure under a unique key based on the input CSS or transformation rules, then retrieve it for subsequent requests without reprocessing.
How can I use this package for dark mode toggles or user-specific CSS overrides in Laravel?
Parse the base CSS once, then dynamically modify rules (e.g., `background-color`) based on user preferences or theme settings. Use Blade directives like `{{ Css::applyTheme($userTheme) }}` to inject runtime styles into your layout.
Will this package break if my CSS contains invalid syntax or legacy hacks (e.g., IE filters)?
By default, the parser is lenient and skips invalid syntax. For strict validation, enable strict mode with `Settings::create()->beStrict()`, but note this may fail on legacy CSS like unquoted URLs or IE-specific filters. Use lenient mode for broader compatibility.
Can I use sabberworm/php-css-parser to scope CSS for Laravel Livewire/Alpine components?
Yes. Parse the CSS, then modify selectors to include unique IDs or classes (e.g., `.livewire-component-123`). Re-render the CSS with scoped rules and inject it into the component’s template or inline styles.
How do I test CSS transformations to ensure they work as expected?
Write unit tests using PHPUnit to verify rule modifications (e.g., check if `font-*` rules are removed). For visual regression, compare the output CSS against a golden master file or render it in a headless browser and assert the DOM matches expectations.
Are there alternatives to sabberworm/php-css-parser for CSS manipulation in Laravel?
Alternatives include `league/css-to-inline-styles` (for inline CSS) or `postcss` (via Laravel Mix). However, sabberworm/php-css-parser is lightweight, pure PHP, and offers fine-grained control over CSS parsing and transformation, making it ideal for Laravel’s ecosystem.
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.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
ecotone/kafka
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata