Remove comments in a php script

This script remove a comments from another php script.
Run script from shell and add name of the php script as argument.

<?php
    $source = $argv[1];
    $fileStr = file_get_contents($source);
    $newStr  = '';
 
    $commentTokens = array(T_COMMENT);
 
    if (defined('T_DOC_COMMENT'))
        $commentTokens[] = T_DOC_COMMENT;   
	
	if (defined('T_ML_COMMENT'))
        $commentTokens[] = T_ML_COMMENT;
    
	$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;
    }
 
    $handle = fopen($source, "w");
    if($handle){
        fwrite($handle, $newStr);
        fclose($handle);
        //echo "open";
    }else{
        echo "no fucking handle!!";
    }
?>

Leave a Reply

Your email address will not be published. Required fields are marked *