PHP code example of jbsnewmedia / css-purger

1. Go to this page and download the library: Download jbsnewmedia/css-purger library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jbsnewmedia / css-purger example snippets


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

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;
    }
}