How to remove JS comments using PHP?
This question is updated: Nov. 4 2013 and answered by: Alexander Yancharuk
But there is a problem right now. A new code: id = id.r
Alexander Yancharuk solution is nearly perfect, he just forgot to ignore double quotes, making his answer break on jquery.min.js which contains the code : replace(Fb,yb[1]+"//")
Please find below the corrected version :
$output = "
//remove comment
this1 //remove comment
this2 /* remove comment */
this3 /* remove
comment */
this4 /* * * remove
* * * *
comment * * */
this5 http://removecomment.com
id = id.replace(/\//g,''); //do not remove the regex //
HTTP+'//www.googleadservices.com/pagead/conversion'
";
$pattern = '/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?
I used this to create a on-the-flow JS compression in PHP based on Manas Tungare example for CSS : http://manas.tungare.name/software/css-compression-in-php/
//
"./js/jquery-1.11.3.min.js",
//
"./js/ie10-viewport-bug-workaround.js",
//
"./js/jasny-bootstrap.min.js",
//
//
"./js/jquery-polyglot.language.switcher.js"
);
/**
* Ideally, you wouldn't need to change any code beyond this point.
*/
$buffer = "";
foreach ($cssFiles as $cssFile) {
$buffer .= file_get_contents($cssFile);
}
// Remove comments
$buffer = preg_replace('/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?