Reputation: 11780
The below code removes all newlines and spaces from a CSS file. But the problem is if the CSS file has something like this:
.sample {
padding: 0px 2px 1px 4px;
}
Output will be :
.sample{padding:0px2px1px4px;}
I want that spaces in between (0px 2px 1px 4px).
Here's the code that I have used :
$str=file_get_contents('sample.css');
//replace all new lines and spaces
$str = str_replace("\n", "", $str);
$str = str_replace(" ", "", $str);
//write the entire string
file_put_contents('sample.css', $str);
Upvotes: 5
Views: 15572
Reputation: 159
There are two ways.
Using ready-made libraries such as minify and CSSMin .
Writing the method manually :
function minify_css($css) {
// Remove comments
$css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css);
// Remove spaces before and after selectors, braces, and colons
$css = preg_replace('/\s*([{}|:;,])\s+/', '$1', $css);
// Remove remaining spaces and line breaks
$css = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '',$css);
return $css;
}
Upvotes: 11
Reputation: 1052
I wrote this small preg_replace
:
$css = preg_replace(
array('/\s*(\w)\s*{\s*/','/\s*(\S*:)(\s*)([^;]*)(\s|\n)*;(\n|\s)*/','/\n/','/\s*}\s*/'),
array('$1{ ','$1$3;',"",'} '),
$css
);
It collapses everything appropriately - leaving spaces within the property definition, but does not remove comments.
Upvotes: 1
Reputation: 1374
Here's a simple yet effective solution:
// Get style contents
$style = file_put_contents('your_style.css');
// Minify it
$minified = $style;
$minified = str_replace("\n", "", $minified);
$minified = str_replace(" ", " ", $minified);
$minified = str_replace(" ", " ", $minified);
$minified = str_replace(" {", "{", $minified);
$minified = str_replace("{ ", "{", $minified);
$minified = str_replace(" }", "}", $minified);
$minified = str_replace("} ", "}", $minified);
$minified = str_replace(", ", ",", $minified);
$minified = str_replace("; ", ";", $minified);
$minified = str_replace(": ", ":", $minified);
// Write it
header("Content-type: text/css; charset: UTF-8");
echo $minified;
It will remove all empty spaces that are not necessary. It won't however remove comments or replace 4 numbers to 1 where it could be replaced, but it does great job however. Let me know if I am missing something or if you have any ideas how comment removal and/or other functions could be easily added to it.
Upvotes: 1
Reputation: 47111
For minifying CSS in PHP, it's best to use Steve Clay's Minify library. There's no point in reinventing the wheel.
Here's a brief walkthrough on how to install and configure the library.
Upvotes: 6
Reputation: 9284
In your code, adding the following line:
$str = preg_replace("/([0-9]*px(?!;))/", "$1 ", $str);
will add a space to any px string followed by something that is not a ;
This way you fix your code by adding spaces where you pointed out.
$str=file_get_contents('sample.css');
//replace all new lines and spaces
$str = str_replace("\n", "", $str);
$str = str_replace(" ", "", $str);
$str = preg_replace("/([0-9]*px(?!;))/", "$1 ", $str);
//write the entire string
file_put_contents('sample.css', $str);
You could make use of any Php compression library like minify which offer complete css compression options.
I hope this helps.
Upvotes: 3
Reputation: 399
If you want to remove the tabs and spaces around each line, but keep the spaces inside the styles. You should just explode()
the whole content with \n
as the token delimiter and iterate over each line and use php's trim() on it, then implode()
it without any delimiter.
Upvotes: 2