CodeGrabber
{ USER }
posts: 23
last: 28-Apr-2008
TITLE: PHP function to optimize a CSS file
DESCRIPTION: PHP function to optimize a CSS file
Submitted: 28-Nov-2007 04:50:27 ( 1yrs 5w 6d 4h ago ) Language: CSS (*.css)
Views: 315 Lines of Code: 22 LINES
Rating:
rate: star1
star2
star3
star4
star5
dstar1
dstar2
dstar3
dstar4
dstar5  ( rated! )
  { 0.00 / 5 }
Difficulty: Intermediate
Bookmark
/**
	 * 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;
	}