Removing comments from a JS / CSS file using [PHP]

I am building a PHP script to minimize CSS / Javascript , which (obviously) includes getting rid of comments from the file. Any ideas how to do this? (Preferably, I need to get rid of the comments /**/ and // )

+4
source share
3 answers

This wheel was invented - http://code.google.com/p/minify/ .

+3
source

Template for deleting comments in JS

 $pattern = '/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/'; 

CSS Removal Template

 $pattern = '!/\*[^*]*\*+([^/][^*]*\*+)*/!'; $str = preg_replace($pattern, '', $str); 

I hope the above should help someone ..

REFF: http://castlesblog.com/2010/august/14/php-javascript-css-minification

+2
source

Take a look at minify , "heavy regex- based white space removal, unnecessary comments and tokens."

+1
source

Source: https://habr.com/ru/post/1333049/


All Articles