Any program to automatically remove comments?

I comment all of my code - html, css, php, javascript - a ton, but I would like to remove it before putting it into a production environment. Is there a quick and painless software program (or any method) to do this?

I do not find unless you hand-made

For your javascript you could use a minifier, find them by Googling ‘javascript minifier’.
Similarly for CSS use a CSS Compressor.
My personal preference is http://www.refresh-sf.com/yui/

For HTML you could write a PHP script which read the html files and use regular expression to locate and remove the comment blocks.
As for removing comments in PHP script, that’s something I’ve never looked into so you’re on your own with that one. Are they doing any harm?

How big are your PHP comments? If you were to remove them would your script file sizes be dramatically reduced?

Cheers

I will have to look into it some more then for php. What you suggested might be an interesting regular expression exercise. Thanks for the other suggestions. I comment all the time, in part for best practices, and mostly because I script a lot of programs/run a lot of tests just to see what will happen. So depending on the type of exercise (php, css, js) my comments can sometimes be longer than the program itself, especially on a character by character basis. (I remove some of the more obvious comments before production, but there’s often a lot left.)

Are you on Mac? bbEdit is certainly capable of what you’re looking for in the right hands - and is a bargain in the app store these days :slight_smile:

Currently using Windows, so it won’t for me. :frowning: Good suggestion though.

If you don’t mind a bit of a brute force attack, Microsoft Word can do this. I’d recommend recording a macro, because it’s pretty tedious, and that way you only need to go through the whole process once for each file type, and after that you can just press a button and have it all done by magic.

The key is to know that in the find and replace function, ^? represents any character.

So paste your HTML code into Word, and then do a serious of find and replace operations, in each case replacing the string with an empty string.
Start with <!-- ^? –> then <!-- ^?^? –> then <!-- ^?^?^? –> and so on.

It helps if you know what the shortest and longest comments you’ve used are, and that way if you know that you’ve never left a comment with fewer than three characters, you can miss the first couple of replacements out.

Same principle for comments in other languages, except that obviously you have to change the delimiters.

Microsoft Word… it makes me laugh, but it’s true.

Just about any program with regex can do that as well.

For PHP comments, I wouldn’t worry about removing the comments. They cause such a small overhead that it isn’t a big deal.

For CSS, you can do a fairly simple regex to remove it. It differs depending what you are using, but if you did it with PHP, it’d be:
‘/\/\.\*\//Um’

For PHP can use PHP itself from the command line…
http://us2.php.net/manual/en/features.commandline.options.php


> php --strip file.php > new_file.php

Thanks everyone. Excellent suggestions. I hadn’t considered Word macros, and I didn’t know about the strip file ability of php.

samanime may mock me for saying this, but I have often used Word to generate HTML. Not, I hasten to add, by using the “save as HTML” option, because that’s on a par with rooting in bins for your dinner, but by using it as a plain text editor with a good search-and-replace function. Other useful ‘special characters’ are ^p (line break) and ^t (tab), which together have saved me countless hours when converting large tables into HTML, without having to go through WYSINWYG code stripping out all the crud. I’m sure there are other programs out there that can do it better, but I don’t have them on my computer, whereas I do have an ancient, creaking but still functional copy of Word.