Source Code trimmen

Beispiel ausführen

Diese Funktion nutzt den Tokenizer um den Sourcecode durch weglassen von Whitespace und Kommentaren zu komprimieren.

Achtung, das ganze funktioniert nur sehr rudimentär. Aktuell gibt es z.B. bei Dollar und Backslashes innerhalb von Strings Probleme. Wie man sie oft bei rexexp-pattern hat. Also immer testen ob der Code nach dem komprimieren auch wirklich noch funktioniert.

<?php
/**
 * TrimSource()
 */
    
function TrimSource($source)
    {

        
$tokens token_get_all($source);

        
// Init
        
$source '';
        
$was_ws false;
    
$nl '';
    
$spacer '';
    
$beforespacer '';    
        
// Process
        
foreach ($tokens as $token) {
            if (
is_string($token)) {
                
// Single character tokens
                
$source .= $token.$nl;
        
$nl '';
            } else {
                list(
$id$text) = $token;
                
//        echo token_name($id).' ';
                
switch ($id) {
                    
// Skip all comments
                    
case T_COMMENT:
                    case 
T_ML_COMMENT:
//                    case T_DOC_COMMENT:
                        
break;

            case 
T_END_HEREDOC:
                        
$source .= $text;
            
$nl "\n";
            break;        


                    
// Remove whitespace
                    
case T_WHITESPACE:
                        
// We don't want more than one whitespace in a row replaced
                        
if ($was_ws !== true) {
                            
$source .= '';
                        }
                        
$was_ws true;
                        break;
/*
           case T_STRING:
$source .= 'String Content';

            break;
*/
            
case T_AS:
                
$beforespacer ' ';
            case 
T_CASE:
            case 
T_RETURN:
            case 
T_FUNCTION:
                    case 
T_CLASS:
                
$spacer ' ';

                    default:
                        
$was_ws false;
                        
$source .= $beforespacer.$text.$spacer;
            
$spacer '';
            
$beforespacer '';
                        break;
                }
            }
        }

        return 
$source;
    }





$size_uncompressed 0;
$theValue '';



if (!empty(
$_POST['theValue'])){

    
$_POST['theValue'] = get_magic_quotes_gpc()
        ? 
stripslashes($_POST['theValue'])
        : 
$_POST['theValue'];

   
$size_uncompressed strlen($_POST['theValue']);
   
$theValue htmlentities($_POST['theValue']);


    
$jsw TrimSource($_POST['theValue']);

    
$size_compressed strlen($jsw);


    
$percent_compressed number_format($size_compressed $size_uncompressed *100,2,',','.');
    
$data htmlentities($jsw);

    if (!isset(
$_POST['doitnow'])) {
        
$jsw '';
    }else{
        
$jsw '<script type="text/javascript">'."\n".$jsw."\n</script>";
    }

echo <<< EOT
<h2>$size_compressed $percent_compressed %</h2>
<pre style="background-color:#e1e1e1;padding:20px;border:2px solid #000;">
$data
</pre>

EOT;







echo <<< EOT
<h2>$size_uncompressed 100%</h2>
<form action="
{$_SERVER['PHP_SELF']}" method="post" enctype="multipart/form-data">
<textarea style="display:block;" cols="50" rows="10" name="theValue">
$theValue</textarea>
<input  style="display:block;" type="submit" value="Snippet hochladen">
</form>
EOT;

?>

Benutzte Funktionen


Hinweise zum DatenschutzImpressum © 2005-2008 S. Eickhoff