Remove comments from PHP

Paste a PHP file with //, #, or /* */ comments in it. This strips all three styles and leaves the rest of the file alone.

PHP
With comments
Loading editor…
Cleaned
Cleaned PHP will appear here.

Why PHP files collect so many comments

PHP codebases tend to live a long time, and a file that's been touched by a few different developers usually shows it: a commented-out query next to the one that replaced it, a shell-style # note from whoever was more used to writing config files that week, a // TODO that's been there since the last framework upgrade. Clearing it out makes the actual logic easier to follow.

It's also a normal step before minifying a script for production, or before pasting a function somewhere that doesn't need your internal notes attached to it.

The three comment styles PHP allows

PHP supports // and # for a line comment, and /* */ for a block. All three get removed here. Anything written inside a string, a heredoc, or a nowdoc block is left exactly as it was, since it isn't a comment even if it contains one of those characters.

Frequently asked questions

PHP has three comment styles, does this handle all of them?

Yes. // and # both start a line comment, and /* */ wraps a block. All three are removed; only real comments, never anything inside a string.

Will it touch the <?php and ?> tags?

No, those are left exactly where they are. Only comment text is removed, the structure of the file stays the same.

Is this safe for a file that mixes PHP and HTML?

Yes. It only strips PHP-style comments inside PHP blocks. Any HTML comments in the same file need the separate HTML comment remover.