SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2011
    Posts
    174
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?

  2. #2
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do not find unless you hand-made

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    SitePoint Zealot
    Join Date
    Nov 2011
    Posts
    174
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.)

  5. #5
    SitePoint Wizard silver trophy TheOriginalH's Avatar
    Join Date
    Aug 2000
    Location
    Thailand
    Posts
    4,811
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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
    ~The Artist Latterly Known as Crazy Hamster~
    922ee590a26bd62eb9b33cf2877a00df
    Currently delving into Django, GIT & CentOS

  6. #6
    SitePoint Zealot
    Join Date
    Nov 2011
    Posts
    174
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Currently using Windows, so it won't for me. Good suggestion though.

  7. #7
    Mouse catcher silver trophy
    Stevie D's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    5,822
    Mentioned
    110 Post(s)
    Tagged
    1 Thread(s)
    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.

  8. #8
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    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'

  9. #9
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    For PHP can use PHP itself from the command line...
    http://us2.php.net/manual/en/feature...ne.options.php

    Code:
    > php --strip file.php > new_file.php
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  10. #10
    SitePoint Zealot
    Join Date
    Nov 2011
    Posts
    174
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everyone. Excellent suggestions. I hadn't considered Word macros, and I didn't know about the strip file ability of php.

  11. #11
    Mouse catcher silver trophy
    Stevie D's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    5,822
    Mentioned
    110 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by ShinVe View Post
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •