benlumley benlumley - 4 years ago 337
PHP Question

Best way to automatically remove comments from PHP code

Whats 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.


I want this:

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

to become:

if ($whatsit) {
echo '<html>Some embedded HTML</html>';

(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 - thats whats tripped up the things that have come up on google.

Answer Source

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))

        $token = $token[1];

    $newStr .= $token;

echo $newStr;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download