Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to automatically remove comments from PHP code

What’s the best way to remove comments from a PHP file?

I want to do something similar to strip-whitespace() - but it shouldn't remove the line breaks as well.

For example,

I want this:

<?PHP // something if ($whatsit) {     do_something(); # we do something here     echo '<html>Some embedded HTML</html>'; } /* another long comment */ some_more_code(); ?> 

to become:

<?PHP if ($whatsit) {     do_something();     echo '<html>Some embedded HTML</html>'; } some_more_code(); ?> 

(Although if the empty lines remain where comments are removed, that wouldn't be OK.)

It may not be possible, because of the requirement to preserve embedded HTML - that’s what’s tripped up the things that have come up on Google.

like image 995
benlumley Avatar asked Feb 02 '09 16:02

benlumley


People also ask

How do I block comments in PHP?

Answer: Use the Syntax "// text" and "/* text */" Comments are usually written within the block of PHP code to explain the functionality of the code.

Why is my PHP code commented out in HTML?

So because PHP tags are not valid in HTML files, when not preprocessed by the server, the browser doesn't recognise it, so it automatically converts it to comments since it doesn't know what else to do with it.


2 Answers

I'd use tokenizer. Here's my solution. It should work on both PHP 4 and 5:

$fileStr = file_get_contents('path/to/file'); $newStr  = '';  $commentTokens = array(T_COMMENT);      if (defined('T_DOC_COMMENT')) {     $commentTokens[] = T_DOC_COMMENT; // PHP 5 }  if (defined('T_ML_COMMENT')) {     $commentTokens[] = T_ML_COMMENT;  // PHP 4 }  $tokens = token_get_all($fileStr);  foreach ($tokens as $token) {         if (is_array($token)) {         if (in_array($token[0], $commentTokens)) {             continue;         }                  $token = $token[1];     }      $newStr .= $token; }  echo $newStr; 
like image 108
Ionuț G. Stan Avatar answered Sep 21 '22 18:09

Ionuț G. Stan


Use php -w <sourcefile> to generate a file stripped of comments and whitespace, and then use a beautifier like PHP_Beautifier to reformat for readability.

like image 37
Paul Dixon Avatar answered Sep 21 '22 18:09

Paul Dixon