Minify CSS Using PHP
icon-search
Minify CSS Using PHP

Minify CSS Using PHP

Minify CSS Using PHP

If you want to compress your CSS code and decrease your CSS file size use this PHP function to do that.


Using this PHP function you can easily compress CSS code.


To run this function you just have to enter your CSS file location and you will get compressed code in a few seconds.


PHP Code To Minify CSS:

<?php
function minimizeCSS($filename){
$css = file_get_contents($filename);
$css1 = preg_replace('/\/\*((?!\*\/).)*\*\//', '', $css); // negative look ahead
$css2 = preg_replace('/\s{2,}/', '', $css1);
$css3 = preg_replace('/\s*([:;{}])\s*/', '$1', $css2);
$css4 = preg_replace('/;}/', '}', $css3);
$myfile = fopen($filename, "w") or die("Unable to open file!");
fwrite($myfile, $css4);
fclose($myfile);}
minimizeCSS('style.css');
?>

Replace style.css with your css file name.
Disqus Codes

No comments

Notifications

Disqus Logo