Compress and output CSS

Have you ever wanted to reduce the size of your CSS files that you serve to your visitors? If so then here is a quick solution to it. This function will remove all unnecessary spaces, tabs, new line characters as well as multi- and single-line comments reducing the size of your CSS file quite significantly.

Here is the function source:

  1. <?php
  3. /**
  4.  * Compress contents of a CSS file
  5.  *
  6.  * @param string $css Contents of a css file
  7.  * @return string
  8.  */
  9. function compressCss($css)
  10. {
  11.     // remove multiline comments, new lines, tabs and single line comments
  12.     $css = preg_replace_callback('/(\/\*.*?\*\/|\n|\t|\/\/.*?\n)/sim',
  13.         create_function(
  14.             '$matches',
  15.             'return "";'
  16.         ), $css);
  18.     // remove all around in ",", ":" and "{"
  19.     $css = preg_replace_callback('/\s?(,|{|:){1}\s?/sim',
  20.         create_function(
  21.             '$matches',
  22.             'return $matches[1];'
  23.         ), $css);
  25.     return $css;
  26. }

Improved Zend_Debug::dump() and var_dump() functions

The native Zend_Debug::dump() function is a step forward from the usual approach to dumping variables to debug your code (usually by using var_dump or print_r). However, as always, things can be improved and that is what we will try to do in this article.

Extract all keys from a multidimensional array

Although PHP boasts a huge number of array related functions, unfortunately there is no native function to extract keys from a multidimensional array because array_keys only supports one-dimensional arrays.

For those occasions when you need it, array_keys_multi might help you.

Multiple parameters in Zend_Db::quoteInto()

Zend_Db has a nice function called quoteInto(). As it is described in the documentation - "The method simply interpolates the value into the string, escapes special characters, and applies quotes around it.".

Let's say you have an sql query which looks like this:

  1. SELECT * FROM users WHERE enabled = ? AND country = ?

You wouldn't be able to use the native quoteInto function to replace both question marks in the sql without processing the same string twice:

  1. $users = new Users();
  2. $sql = 'SELECT * FROM users WHERE enabled = ? AND country = ?';
  3. $sql = $users->getAdapter()->quoteInto($sql, 1);
  4. $sql = $users->getAdapter()->quoteInto($sql, 'UK');

This would get you

  1. SELECT * FROM users WHERE enabled = 1 AND country = 'UK'

Unfortunately this is not the best way to do it. Imagine what the code would look like if you needed to replace 3, 5 or even more placeholders.

This is where our custom extension to Zend_Db_Table_Abstract comes in handy.

Add array capabilities to DOMDocument class

Unfortunately the native PHP's DOMDocument class does not allow you to create an xml document from a multidimensional array or save it to one.

Here is a class, which adds these capabilities - by using DOMDocumentExt you can now call loadArray or saveArray functions to do the loading and saving tasks respectively.

As an added bonus you can also use function called getPathValue, which accepts one parameter - $path - a string in root/child1/child2/../childN format. It will convert the current document tree to an array and will return the contents of the specified path if such exists (or null if such path does not exist).

Showing 11 - 15 of 32 results