PHPackages                             jbsnewmedia/css-purger - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. jbsnewmedia/css-purger

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jbsnewmedia/css-purger
======================

A lightweight PHP library to purge unused CSS rules from stylesheets based on defined selectors.

1.0.4(4mo ago)2241MITPHPPHP &gt;=7.4

Since Jul 16Pushed 3mo agoCompare

[ Source](https://github.com/jbsnewmedia/css-purger)[ Packagist](https://packagist.org/packages/jbsnewmedia/css-purger)[ RSS](/packages/jbsnewmedia-css-purger/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (1)Versions (7)Used By (1)

CssPurger
=========

[](#csspurger)

**CssPurger** is a lightweight PHP library to remove unused CSS rules from large stylesheets. It’s perfect for optimizing CSS frameworks like Bootstrap based on actual usage in your project.

---

🧩 Installation
--------------

[](#-installation)

Install via [Composer](https://getcomposer.org/):

```
composer require jbsnewmedia/css-purger
```

---

🚀 Features
----------

[](#-features)

- Removes unused CSS based on custom-defined selectors
- Supports nested blocks (e.g. `@media`)
- Fully supports selectors with pseudo-classes like `:hover`, `:focus`, etc.
- Output as **minified** or **pretty-printed** CSS
- Easily extendable via subclassing
- No external dependencies
- Bootstrap integration example included

---

🔧 Usage
-------

[](#-usage)

```
use JBSNewMedia\CssPurger\Vendors\Bootstrap;

$purger = new Bootstrap('./assets/css/bootstrap.css');

$purger->loadContent();
$purger->prepareContent();
$purger->runContent();

// Add the selectors you want to keep
$purger->addSelectors([
    ':root',
    '[data-bs-theme=light]',
    '[data-bs-theme=dark]',
    'body',
    'h1',
    '.h1',
    '.container',
    '.pt-3',
    '.pb-3',
    '.alert',
    '.alert-danger',
    '.btn:hover',
]);

// Save the result
file_put_contents('./assets/css/bootstrap-purged.css', $purger->generateOutput(false)); // readable
file_put_contents('./assets/css/bootstrap-purged.min.css', $purger->generateOutput());   // minified
```

---

🧠 Extendability
---------------

[](#-extendability)

You can subclass `CssPurger` to customize parsing or handling, e.g. for Bootstrap-specific structures:

```
use JBSNewMedia\CssPurger\CssPurger;

class CssPurgerBootstrap extends CssPurger
{
    public function prepareContent(): self
    {
        $this->cssBlockPrefix = substr($this->content, 0, strpos($this->content, ':root'));
        $this->content = str_replace("*/\n:root,", "*/\n}\n:root,", $this->content);
        return $this;
    }
}
```

---

⚠️ Notes
--------

[](#️-notes)

- This is not a full CSS parser — it uses lightweight string-based logic for performance
- You must explicitly define which selectors to keep

---

📄 License
---------

[](#-license)

MIT License — free for personal and commercial use.

---

🤝 Contributing
--------------

[](#-contributing)

Pull requests, bug reports and ideas for improvements are always welcome!

---

🧑‍💻 Author
----------

[](#‍-author)

Maintained by [JBS New Media](https://github.com/jbsnewmedia).

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance78

Regular maintenance activity

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~43 days

Total

5

Last Release

124d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/44b85e5ce069ccae08123600bb24204ea8fe99c8cb5cbfae108ff29c179e503d?d=identicon)[jschwind](/maintainers/jschwind)

---

Top Contributors

[![jschwind](https://avatars.githubusercontent.com/u/985564?v=4)](https://github.com/jschwind "jschwind (17 commits)")

---

Tags

bootstrapcleancssminifyoptimizerphppurgephpcssminifycleanbootstrappurgeoptimizer

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jbsnewmedia-css-purger/health.svg)

```
[![Health](https://phpackages.com/badges/jbsnewmedia-css-purger/health.svg)](https://phpackages.com/packages/jbsnewmedia-css-purger)
```

###  Alternatives

[ttskch/select2-bootstrap4-theme

Select2 v4 theme for Bootstrap4

43424.6k2](/packages/ttskch-select2-bootstrap4-theme)[nlac/nlsclientscript

Yii ClientScript extension for prevent reloading javascript and merging/minfying resources

208.2k](/packages/nlac-nlsclientscript)[trentrichardson/cakephp-shrink

Compiles, combines, and minifies javascript, coffee, less, scss, and css

1619.3k](/packages/trentrichardson-cakephp-shrink)[tailwindphp/tailwindphp

A full port of TailwindCSS 4.x to PHP

171.7k1](/packages/tailwindphp-tailwindphp)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
