Skip to content

Instantly share code, notes, and snippets.

@MeanEYE
Created June 5, 2016 19:02
Show Gist options
  • Save MeanEYE/36d4abe94ea99014284628a50f5a6d9b to your computer and use it in GitHub Desktop.
Save MeanEYE/36d4abe94ea99014284628a50f5a6d9b to your computer and use it in GitHub Desktop.
Simple CSS optimizer in PHP.
<?php
/**
* Simple CSS minifier.
* Copyright (c) Mladen Mijatov, Way2CU
*/
// sample data
$data = <<<END
/**
* Full blown comment.
*/
a.link + span:nth-of-type(3),
a.link ~ p,
footer div.form {
position: relative;
top: 0px;
min-height: 700px;
width: calc((100% - 1200px) / 2);
/* inline comment */
background-color: @color_background;
background-image: url(../images/footer-background.jpg);
background-position: top center;
background-repeat: no-repeat;
color: #ffbb00;
color: rgba(0.0, 0.0, 0.0, 1);
}
END;
$data = preg_replace('/\/\*.*?(?=\*\/)\*\//imus', '', $data);
$data = preg_replace('/([^\d])-?(0+)(px|pt|rem|em|vw|vh|vmax|vmin|cm|mm|m\%)/imus', '\1\2', $data);
$data = preg_replace('/\s*([>~:;,\[\]\{\}])\s*/imus', '\1', $data);
$data = preg_replace('/\s*([\(\)])\s*([^+-\/\*\^])/imus', '\1\2', $data);
$data = preg_replace('/([\+])\s*([^\d])/imus', '\1\2', $data);
$data = preg_replace('/#([\dabcdef])\1([\dabcdef])\2([\dabcdef])\3/imus', '#\1\2\3', $data);
print preg_replace('/;\}/imus', '}', $data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment