I was looking earlier for a way to strip php comments from my source files but didn't come up with much. I wrote the following function to do the trick using the tokenizer. I've tested in on an entire phpMyAdmin install and it worked fine afterward... so it should be good to go. You may also specify any number of tokens to strip such as T_WHITESPACE rather the default of T_COMMENT and T_DOC_COMMENT.
Hopefully someone finds it useful.
<?php
function strip_tokens($code) {
$args = func_get_args();
$arg_count = count($args);
if( $arg_count === 1 ) {
$args[1] = T_COMMENT;
$args[2] = T_DOC_COMMENT;
}
for( $i = 1; $i < $arg_count; ++$i )
$strip[ $args[$i] ] = true;
$newlines = array("\n" => true, "\r" => true);
$tokens = token_get_all($code);
reset($tokens);
$return = '';
$token = current($tokens);
while( $token ) {
if( !is_array($token) )
$return.= $token;
elseif( !isset($strip[ $token[0] ]) )
$return.= $token[1];
else {
for( $i = 0, $token_length = strlen($token[1]); $i < $token_length; ++$i )
if( isset($newlines[ $token[1][$i] ]) )
$return.= $token[1][$i];
}
$token = next($tokens);
} return $return;
} ?>