5

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

Dilip Rajkumar
  • 7,006
  • 6
  • 60
  • 76
  • 2
    Ehm... parse the CSS and JS? If you can't do that, use an existing minifier like e.g. google closure compiler for JS. – thejh Dec 22 '10 at 15:10
  • 1
    Not an answer but... it's worth noting that minifying CSS and JavaScript is not trivial. Even if you are happy with the minimum size reduction provided by removing comments and white space, whatever parser you write will probably fail with edge cases. The advise of using a third-party tool is pretty good, seriously. – Álvaro González Dec 22 '10 at 15:32

4 Answers4

5

Pattern for remove comments in JS

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

Pattern for remove comments in CSS

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

$str = preg_replace($pattern, '', $str);

I hope above should help someone..

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

Dilip Rajkumar
  • 7,006
  • 6
  • 60
  • 76
  • 1
    This pattern for removing comments from css is the only one that worked for me (completely). Thanks! – Chris May 27 '14 at 03:11
4

That wheel has been invented -- https://github.com/mrclay/minify.

Mr. Ronald
  • 687
  • 4
  • 13
James Sumners
  • 14,485
  • 10
  • 59
  • 77
  • 4
    It _is_ an answer to the question. The problem he is trying to solve has already been solved. If he doesn't want to use that solution, he can at least look at it for inspiration. – James Sumners Dec 22 '10 at 15:34
  • In particular the classes Minify_CSS_Compressor and JSMin. The 2.1.4 branch has the lasted versions. – Steve Clay Jan 07 '11 at 03:07
2

PLEASE NOTE - the following approach will not work in all possible scenarios. Test before using in production.

Without preg patterns, without anything alike, this can be easily done with PHP built-in TOKENIZER. All three (PHP, JS and CSS as well) share the same way of representing comments in source files, and PHP's native, built-in token_get_all() function (without TOKEN_PARSE flag) can do dirty trick, even if the input string isn't well formed PHP code, which is exactly what one might need. All it asks is <?php at start of the string and magic happens. :)

<?php 
function no_comments (string $tokens)
{   // Remove all block and line comments in css/js files with PHP tokenizer.
    $remove = [];
    $suspects = ['T_COMMENT', 'T_DOC_COMMENT'];
    $iterate = token_get_all ('<?php '. PHP_EOL . $tokens);

    foreach ($iterate as $token) 
    {
        if (is_array ($token)) 
        {
            $name = token_name ($token[0]); 
            $chr = substr($token[1],0,1);

            if (in_array ($name, $suspects) 
            && $chr !== '#') $remove[] = $token[1];
        }
    }

    return str_replace ($remove, '', $tokens);
}

The usage goes something like this:

echo no_comments ($myCSSorJsStringWithComments);
Spooky
  • 1,235
  • 15
  • 17
1

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

schellack
  • 10,144
  • 1
  • 29
  • 33