CSI: PHP

"Looking at your tweets I cannot even fathom what your job is. CSI:PHP?" — @grmpyprogrammer

style.css.php

| Comments

The concept makes sense – minify static assets) in order to make your pages smaller and therefore more responsive. The execution leaves quite a lot to be desired.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php

// minifying the CSS
header('Content-type: text/css');
ob_start("compress");

function compress($buffer) {
    // remove comments
    $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
    // remove tabs, spaces, newlines, etc.
    $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
    return $buffer;
}
?>

body {
    background-color: #FFF;
    color: black;
    margin: 0;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 10px;
}

table {
    margin: 0;
    padding: 0;
    border: 0;
}

// . . . snip . . .

<?php ob_end_flush(); ?>

Here are a few better ideas:

  • Don’t minimize anything at all. Is your site really that highly trafficked?
  • Manually minimize your CSS and JavaScript files using freely available tools.
  • Combine and minify your static assets during the build process using something like Phing.

Comments