Skip to content

Instantly share code, notes, and snippets.

@slater
Created November 24, 2014 04:24
Show Gist options
  • Save slater/96197c7a32d502a29b5d to your computer and use it in GitHub Desktop.
Save slater/96197c7a32d502a29b5d to your computer and use it in GitHub Desktop.
CSS minimizer and de-commentor in PHP
<head>
<title>CSS Minimizer and de-commentor</title>
<style type="text/css">
* {margin:0; padding:0; border:0; box-sizing:border-box; outline:0; font-family: sans-serif; font-size:medium !important;}
body {width:50%; margin:1rem auto;}
textarea, input {border:1px solid #ccc; border-radius:3px 3px; margin:0 0 1rem 0; padding:1rem;}
input[type=submit] {width:50%; margin:0 auto; display: block; border-bottom:1px solid #aaa; cursor: pointer; box-shadow: 0 3px 7px rgba(0,0,0,0.1); background-color:#eee;}
input[type=submit]:hover {border:1px solid #aaa; background-color:#aaa; color:#fff;}
</style>
</head>
<body>
<form action="" method="post">
<textarea name="post" style="width:900px; height:500px; font-family:monospace;">
<?php
if($_POST)
{
$str= preg_replace( '#/\*(?:.(?!/)|[^\*](?=/)|(?<!\*)/)*\*/#s' , ' ' , $_POST['post']);
$str = str_replace("\t","", $str);
$str = str_replace(" ","", $str);
$str = str_replace(";\r","; ", $str);
$str = str_replace("; \r","; ", $str);
$str = str_replace("; \n","; ", $str);
$str = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $str);
$str = str_replace(" ","", $str);
$str = str_replace(" ","", $str);
$str = str_replace("{ ","{", $str);
$str = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $str);
$str = str_replace("{\r","{", $str);
$str = str_replace("{\n","{", $str);
$str = str_replace("\n}","}", $str);
$str = str_replace("\r}","}", $str);
$str = str_replace(" }","}", $str);
$str = str_replace("\r ","\r", $str);
$str = str_replace("\n ","\n", $str);
$str = str_replace(",\n",", ", $str);
$str = str_replace(",\r",", ", $str);
$str = str_replace(": ",":", $str);
$str = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $str);
$str = str_replace("\r}","}", $str);
$str = str_replace("\r{","{", $str);
$str = str_replace("\g{","{", $str);
$str = str_replace("\n{","{", $str);
$str = str_replace("\g}","}", $str);
$str = str_replace("\n}","}", $str);
$str = str_replace("\\","", $str);
echo $str;
}
?></textarea><br/>
<input type="submit">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment