Here’s a PHP function for reformatting the whitespace in PHP code:
\n”;
$indentlevel = 0;
$output = ”;
foreach(token_get_all($sourcecode) as $token)
{
if (is_array($token))
list($tokentype, $spelling) = $token;
else
list($tokentype, $spelling) = array(NULL, $token);
if ($tokentype==T_WHITESPACE && preg_match(‘/\s$/’, $output))
continue;
elseif ($tokentype==T_WHITESPACE)
$spelling = ‘ ‘;
if ($spelling=='{‘)
{
$indentlevel++;
if ($outmode==SC_IMPROVED_BRACES)
$output .= “\n$indent”;
elseif ($outmode==SC_WEIRDASS_BRACES)
$output .= “\n$indent\t”;
}
elseif ($spelling==’}’)
{
$indentlevel–;
$output = preg_replace(‘/\t$/i’, ”, $output);
if ($outmode==SC_WEIRDASS_BRACES)
$output .= “\t”;
}
$indent = str_repeat(“\t”, $indentlevel);
$output .= $spelling;
if ($spelling==’;’||$spelling=='{‘||$spelling==’}’)
$output .= “\n$indent”;
}
return $output;
}
$sc = ‘echo “1”;if (FALSE){echo 2;
echo 2; echo 2; if( TRUE ){echo 3;}}echo 4;’;
print sc_pp($sc, 1);
1?>