Well, T_WHITESPACE can be a space or a newline, etc. Thus, one of the trivial approaches would be to automatically replace all instances of T_WHITESPACE with a new one consisting of exactly one space.
But for a more sensible method, just go to the list of parser tokens and find out which ones should have spaces after it and which shouldn't (something like this):
foreach ($tokens as $k => $val) { if (is_array($val) && $val[0] == T_WHITESPACE) { if (!is_array($tokens[$k - 1])) { //remove this space } else { switch ($tokens[$k - 1][0]) { case T_ABSTRACT: case T_FUNCTION: //.. other keeps here: continue; break; default: //remove the space } } } }
And one more note, do not do this for performance. If you use the OPCODE cache (e.g. APC), you will not see any benefit to a lot of work. If you are not using it, why not be?
source share