Сшивание файлов css и минимизация на лету

Я сшиваю файлы css для повышения производительности.

Теперь я хочу включить функцию минимизации, как показано ниже. Как я могу это включить?

function minify( $css ) {
    $css = preg_replace( '#\s+#', ' ', $css );
    $css = preg_replace( '#/\*.*?\*/#s', '', $css );
    $css = str_replace( '; ', ';', $css );
    $css = str_replace( ': ', ':', $css );
    $css = str_replace( ' {', '{', $css );
    $css = str_replace( '{ ', '{', $css );
    $css = str_replace( ', ', ',', $css );
    $css = str_replace( '} ', '}', $css );
    $css = str_replace( ';}', '}', $css );

    return trim( $css );
}

КОД ДЛЯ СШИВАНИЯ:

<?php
$filename = $_GET['files']; 
// validate that $filename is set, contains only legal characters  
// and does not contain multiple dots (potential sign of trouble) 
if (!$filename || 
        !preg_match('/^([\.\-\_a-z0-9]*)$/i', $filename) || 
        preg_match('/([\.]{2,})/', $filename))     
    exit(); 

// we're sending CSS back to the browser 
header('Content-Type: text/css'); 

$files = explode('-', $filename, 15); 

// we're also writing CSS to a subdirectory "cache" 
// the filename will be the hyphen-delimited value  
// of $filename 
$cachefile = @fopen('cache/'. $filename, 'w'); 

// loop through, read each file, and stitch them together
foreach ($files AS $file) {
    $fcontents = null;     
    if ($cssfile = fopen($file, 'r')) {         
        $fcontents = fread($cssfile, filesize($file)) ."\n";         
        fclose($cssfile);     
    }

    // if we read something, write it and send it to browser     
    if ($fcontents) {
        fwrite($cachefile, $fcontents, strlen($fcontents));
        echo $fcontents;     
    }     
} 

// all done 
fclose($cachefile);
?>
0
задан Mark Henry 30 July 2011 в 16:35
поделиться