Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine css selectors with the same properties, php

I'm looking for a PHP library(script) that will be able to combine CSS selectors with the same properties. Here is what I mean:

.myclass{
    font-size: 14px;
    color: red;
}
.something{
    font-size: 14px;
    color: red;
}

After processing the CSS above the result should be:

.myclass, .something{
    font-size: 14px;
    color: red;
}

Any help is appreciated.

like image 310
Andrei Surdu Avatar asked Mar 13 '16 06:03

Andrei Surdu


1 Answers

A nice php library to do this is https://github.com/Cerdic/CSSTidy

Example code:

<?php
include('class.csstidy.php');

$css_code = '
.myclass{
    font-size: 14px;
    color: red;
}
.something{
    font-size: 14px;
    color: red;
}
';

$css = new csstidy();

$css->$css->set_cfg('merge_selectors', 2);

$css->parse($css_code);

echo $css->print->formatted();
?>

Output:

.myclass,.something {
  font-size:14px; color:red
}
like image 193
Lorenzo Marcon Avatar answered Sep 24 '22 07:09

Lorenzo Marcon