DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

PHP-function To Optimize A CSS-file

06.13.2007
| 3988 views |
  • submit to reddit
        
	/**
	 * Converts a CSS-file contents into one string
	 *
	 * @param    string  $t Text data
	 * @param    int     $is_debug Skip convertion
	 * @return   string  Optimized string
	 */
	function text_smooth_css($t, $is_debug = 0)
	{
		if ($is_debug) { return $t; }
		/* Remove comments */
		$t = preg_replace("/\/\*(.*?)\*\//s", ' ', $t);
		/* Remove new lines, spaces */
		$t = preg_replace("/(\s{2,}|[\r\n|\n|\t|\r])/", ' ', $t);
		/* Join rules */
		$t = preg_replace('/([,|;|:|{|}]) /', '\\1', $t);
		$t = str_replace(' {', '{', $t);
		/* Remove ; for the last attribute */
		$t = str_replace(';}', '}', $t);
		$t = str_replace(' }', '}', $t);
		return $t;
	}
    

Comments

Snippets Manager replied on Fri, 2007/07/13 - 11:15am

You should know that some browsers always require a semicolon at the end of an attribute, even if it's the last one.