简体   繁体   中英

How to remove JS comments using PHP?

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.replace(/\\//g,'');

This is my example:

<?php
$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 //
";

$output = preg_replace( "/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:)\/\/.*))/", "", $output ); //Yancharuk's code/regex
// "/(?<!\:)\/\/(.*)\\n/ = my oldest code

echo nl2br($output);
?>

My Problems;

  1. Something wrong with the this1 line;
  2. The //comments is working but I can't create a codes to remove /* comment */ or by that comment with a line break

Here is the output, recent:



this1
this2
this3
this4
this5 http://removecomment.com
id = id.replace(/\\

Try this:

$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 = '/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\')\/\/.*))/';
$output = preg_replace($pattern, '', $output);

echo nl2br($output);

Result on codepad.org .

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 = '/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\'|\")\/\/.*))/';
$output = preg_replace($pattern, '', $output);

echo nl2br($output);

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/

<?php

/**
 * On-the-fly JS Compression by A. Heiligtag
 * Based on On-the-fly CSS Compression by  Manas Tungare.
 *
 * In order to minimize the number and size of HTTP requests for JS content,
 * this script combines multiple JS files into a single file and compresses
 * it on-the-fly.
 *
 */
$cssFiles = array(
    //<!-- Bootstrap -->
    // <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    "./js/jquery-1.11.3.min.js",
    // <!-- Bootstrap core JavaScript
    "./js/bootstrap.min.js",
    // <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    "./js/ie10-viewport-bug-workaround.js",

     // <!-- Jasny bootstrap -->
    "./js/jasny-bootstrap.min.js",

    // <!-- color picker : https://github.com/istvan-ujjmeszaros/bootstrap-colorpickersliders / http://www.virtuosoft.eu/code/bootstrap-colorpickersliders/ -->
    // <!-- ‘Polyglot’ Language Switcher 2 : http://www.ixtendo.com/polyglot-language-switcher-2/ -->
    "./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('/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\'|\")\/\/.*))/', '', $buffer);
// Remove space after colons
$buffer = str_replace(': ', ':', $buffer);
// Remove space before equal signs
$buffer = str_replace(' =', '=', $buffer);
// Remove space after equal signs
$buffer = str_replace('= ', '=', $buffer);
// Remove whitespace
$buffer = str_replace(array("\r\n\r\n", "\n\n", "\r\r", '\t', '  ', '    ', '    '), '', $buffer);
// Enable GZip encoding.
ob_start("ob_gzhandler");
// Enable caching
header('Cache-Control: public');
// Expire in one day
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 86400) . ' GMT');
// Set the correct MIME type, because Apache won't set it for us
header("Content-type: application/javascript");
// Write everything out
echo($buffer);
?>

To remove JavaScript comments and CDATA blocks (//<![CDATA[ , //]]>) combine alex's soluton https://stackoverflow.com/a/8283600/2910183 and Alexander's Yancharuk solution https://stackoverflow.com/a/19510664/2910183 :

$content = preg_replace('~//<!\[CDATA\[\s*|\s*//\]\]>~', '', $content);
$content = preg_replace('/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\)\/\/[^"\'].*))/', '', $content);

This is my best result, following the first post and searching many sites, this function well works for javascript and css and html content in php

function _compress($html){
   //remove comments
   $html = preg_replace('|(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:)\/\/.*))|', '', $html ); //Yancharuk's code/regex

   // remove tabs, spaces, newlines, etc.
   $html = str_replace(array(PHP_EOL, "\t"), '', $html);

   //remove all spaces
   $html = preg_replace('|\s\s+|', ' ', $html);

   return $html;
}

If you want to remove comments from your client side javascript code then you doing it wrong.

Just use minifier. On top of removing comments, it will remove meaningless whitespaces and shorten all names in script.

For example github: UglifyJS 2

Complex regexp hangs up!!! Use simple ones for work with thousands of lines

$module = preg_replace('/\/\*[^\/]*\*\//', '', $module);
/*
* remove comments like this 
*/

$module = preg_replace('/\/\*\*((\r\n|\n) \*[^\n]*)+(\r\n|\n) \*\//', '', $module);
/**
 * remove comments like this other method
 */
$module = preg_replace('/\n(\s+)?\/\/[^\n]*/', '', $module);
// remove comments like this
$module = preg_replace('/ (\t| )+/', '', $module); 
//double spaces
$module = preg_replace('/([\n])+/', "$1", $module);
//double newlines

I would like to share a regex (PHP) snippet of code I wrote for myself it is also being used in the YIREO sriptmerge plugin for joomla marked as simple code.. To compress javascript code and remove all comments from it. It also works with mootools It is fast (in comparison to other PHP solutions) and does not damage the JavaScript it self and it resolves lots of comment removal issues.

If you click on the link below you find a comment removal script in regex. 

These are 112 lines off code that work together also works with mootools and Joomla and drupal and other cms websites. Tested it on 800.000 lines of code and comments. works fine. This one also selects multiple parenthetical like ( abc(/ nn /('/ xvx /'))"// testing line") and comments that are between colons and protect them. 23-01-2016..! This is the code with the comments in it.!!!!

Click Here

I have just for the fun of it tested this ( var regex=/(ftp|https?):\\/\\//;alert('hello,world'); ) and this /* foo(); // some comment */ /* foo(); // some comment */ and this: "Python's division: 1 // 2" and this //"you dope" example and my compression code those not damage the example above! COMMENT FROM ABOVE:( A stupid parser that uses regular expressions will treat valid JavaScript code as comments!) So Maybe you can also write none stupid parsers with regex???

Please feel free to leave a comment if you find a problem with this script with valid javascript sentences. And any kind of comments tags combined (or not combined) that it does not solve correctly..

Updated needs no more testing 800.000 lines work fine now.!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM