minify icon indicating copy to clipboard operation
minify copied to clipboard

How to remove comments from css

Open ProgrammerNomad opened this issue 7 years ago • 2 comments

Hello

All is working fine,

only i am want to remove/hide all css comments from minify output so please help me.

Thanks

ProgrammerNomad avatar Sep 26 '18 09:09 ProgrammerNomad

I would also like to know how to do this.

AgentSmith0 avatar Mar 03 '22 17:03 AgentSmith0

So you want to only strip comments off your CSS...read on!

How to strip comments only from CSS with minify

This is not something that is supported out-of-the-box, but if you are willing to do some really tiny hacking, you will get it working! This is what you must do:

I suppose you have the source code somewhere, let's say it is in a directory called minify. Open minify/lib/Minify/CSS/Compressor.php and replace the whole function

protected function _process($css)
{
...
}

with this one

protected function _process($css)
{
	// apply callback to all valid comments (and strip out surrounding ws)
	$pattern = '@\\s*/\\*([\\s\\S]*?)\\*/\\s*@';
	$css = preg_replace_callback($pattern, array(get_called_class(), '_commentCB'), $css);

	return trim($css);
}

That's it! Have fun stripping comments only off your CSS. :smiley:

sedimentation-fault avatar Jun 16 '24 09:06 sedimentation-fault