Advertisement
austinh115

[PHP] xat.me css fixer upper

May 14th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.30 KB | None | 0 0
  1. <?php
  2. $myfile = fopen($file = "css.css", "r") or die("Missing or bad CSS file.");
  3. $css = fread($myfile,filesize($file));
  4. fclose($myfile);
  5. $css = trim($css);
  6. $elements = explode("}", $css);
  7. $index = array();
  8. $css = "}";
  9. foreach($elements as $ele) {
  10.     if(strpos($ele, "keyframes") !== false) break;//PUT YOUR KEYFRAMES DOWN BELOW OR IT BREAKS
  11.     $split = explode("{", $ele);
  12.     foreach($split as $k=>$v) {
  13.         $split[$k] = trim($v);
  14.     }
  15.     if(count($split) < 2) {
  16.         if($split[0] != "") $css = $split[0].'}';
  17.         continue;
  18.     }
  19.     if(strpos($split[1], ";") !== false) {
  20.         $splits = array_filter(explode("\r\n", str_replace(";","", $split[1])));
  21.         foreach($splits as $splice) {
  22.             if(!isset($index[$splice])) $index[$splice] = array();
  23.             $index[$splice] = array_merge($index[$splice], explode(",", $split[0]));
  24.         }
  25.     } else {
  26.         if(!isset($index[$split[1]])) $index[$split[1]] = array();
  27.         $index[$split[1]] = array_merge($index[$split[1]], explode(",", $split[0]));
  28.     }
  29. }
  30. foreach($index as $value => $elements) {
  31.     foreach($elements as $k=>$v) {
  32.         $elements[$k] = trim($v);
  33.     }
  34.     $css .= implode(",",$elements) . '{'.implode(":", explode(": ", $value)).'}';
  35. }
  36. //minify it on https://cssminifier.com/ or it takes up too much space, plz
  37. $keyframes = '';
  38. echo substr(preg_replace('/\s+/S', " ", $css.$keyframes), 0, -1).";";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement