2007年7月13日 星期五

PHP function: 壓縮 CSS file

Tsung's Blog 那看到的
PHP function: 壓縮 CSS file

真是獲益良多啊~~

透過此 function 可在吐 CSS 時, 或者在版本 Release 時, 自動壓縮 還是比較方便點~ :)
以下轉載自: PHP-function to optimize a CSS-file
PS: 我對此檔案加了作者, 原始連結資訊 和 簡單排版.


<?php
/**
* Converts a CSS-file contents into one string
* Source Code: http://snippets.dzone.com/posts/show/4137
* @Author: Dmitry-Sh http://snippets.dzone.com/user/Dmitry-Sh
*
* @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;
}
?>

沒有留言: