Compress CSS on the fly

Oct 1st, 2007 by Jared Schwager , ,

In an attempt to further speed up loading times for Grupenet, I decided to compress (remove spaces and comments) my CSS stylesheet. However, instead of applying compression directly to the original file, why not add some PHP code to the stylesheet to compress the CSS dynamically when it is loaded on the server? This way all readability is maintained when editing the original file. This way you can add as much commenting as you need to keep your CSS more organized.

Step 1

Create a new file called .htaccess in the same directory your stylesheet is in. Open your newly created .htaccess file and add the following lines:

<Files style.css>
SetHandler application/x-httpd-php
</Files>

(Replace style.css with the filename of your stylesheet)

Step 2

Open up your stylesheet and add the following to the top of your stylesheet:

<?php
header('Content-type: text/css');
function compress($buffer) {
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer);
return $buffer;
}
ob_start("compress");
?>

Now add this to the bottom of your stylesheet:

<?php ob_end_flush(); ?>

Step 3

That's all there is to it! Now open up your browser and point it to your stylesheet. All the comments and spacing should be removed, and it should look something like this.

Web development freeware

Aug 5th, 2007 by Jared Schwager

In many cases, web development can be quite costly between purchasing image editing software to FTP software. You may not know it, but there is a plethora of great free web development software. I’ve put together a list of a few of the best webdev freeware applications I could find.

Bluefish Bluefish Editor (Windows/Linux/Mac)

If you’re looking for a great HTML editor, Bluefish is what to get. It is not only for editing HTML though. This application is capable of editing everything from PHP to Python and Javascript and even adds syntax highlighting.

Paint.NET Paint.NET (Windows only)

Good and free image editors are hard to come by, but Paint.NET is an exception. This image editor can be classified as somewhat of a Paint Shop Pro replacement as it doesn’t have as many features as Photoshop does.

FileZilla FileZilla (Windows/Linux/Mac)

If there’s one essential web development tool, it’s the FTP client. Unfortunately, good free FTP clients aren’t very common. FileZilla is both free and very easy to use. The beta version is available for all platforms, whereas the stable version is only available on Windows.

Know of any other good web development freeware? Post your favorites!