【问题标题】:Minify CSS using preg_replace使用 preg_replace 缩小 CSS
【发布时间】:2010-11-25 15:25:21
【问题描述】:

我正在尝试使用 preg_replace 缩小多个 CSS 文件。实际上,我只是想从文件中删除任何换行符/制表符和 cmets。以下对我有用:

$regex = array('{\t|\r|\n}', '{(/\*(.*?)\*/)}'); echo preg_replace($regex, '', file_get_contents($file));

但我想在单个多行正则表达式中执行此操作,如下所示:

$正则表达式 =

但是,这根本没有任何作用。有没有办法做到这一点?


编辑:好的,所以我将看看现有的缩小器,但它仍然让我想知道如何做这样的多行正则表达式,因为使用 x-modifier 多行正则表达式即使在 php 中也应该可以正常工作,不是吗?

【问题讨论】:

标签: php regex preg-replace


【解决方案1】:

我不确定你会怎么做,但这是我朋友写的一个脚本,它在缩小 CSS 方面非常快:

function minimize_css($input)
{
    // Remove comments
    $output = preg_replace('#/\*.*?\*/#s', '', $input);
    // Remove whitespace
    $output = preg_replace('/\s*([{}|:;,])\s+/', '$1', $output);
    // Remove trailing whitespace at the start
    $output = preg_replace('/\s\s+(.*)/', '$1', $output);
    // Remove unnecesairy ;'s
    $output = str_replace(';}', '}', $output);
    return $output;
}

【讨论】:

    【解决方案2】:

    有一些实用程序可以满足您的需求并为您节省一个可能存在错误的正则表达式。

    YUI compressor 支持缩小 CSS 和 javascript 文件。

    您可能希望在编写自己的选项之前考虑此选项或其他现有选项。

    【讨论】:

      【解决方案3】:

      这是我个人用于 CSS 的:

      $file_contents = file_get_contents($file);<br />
      preg_replace('@({)\s+|(\;)\s+|/\*.+?\*\/|\R@is', '$1$2 ', $file_contents);
      

      【讨论】:

        【解决方案4】:

        这似乎是重新发明轮子的完美例子。互联网上几乎每个站点都使用 CSS,而且所有大站点都以某种方式对其进行压缩。他们的方法已经过测试和优化。如果不需要,为什么要自己滚动?

        Mike 和 Grumbo 已经给出了具体的建议,但我只是想指出一般原则。

        【讨论】:

        • 是的,因为添加库依赖总是比在项目中添加几行代码要好。 ://
        【解决方案5】:

        据我所知,你不能这样做,因为当你把它分成多行时,你实际上是在改变模式。

        编辑:是的,+1 表示不重新发明轮子。

        【讨论】:

          【解决方案6】:

          这就是我在Samstyle PHP Framework 中使用的:

          $regex = array(
          "`^([\t\s]+)`ism"=>'',
          "`([:;}{]{1})([\t\s]+)(\S)`ism"=>'$1$3',
          "`(\S)([\t\s]+)([:;}{]{1})`ism"=>'$1$3',
          "`\/\*(.+?)\*\/`ism"=>"",
          "`([\n|\A|;]+)\s//(.+?)[\n\r]`ism"=>"$1\n",
          "`(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+`ism"=>"\n"
          );
          $buffer = preg_replace(array_keys($regex),$regex,$buffer);
          

          希望这会有所帮助!

          【讨论】:

            【解决方案7】:
            function minifyCSS($css){
                $css = trim($css);
                $css = str_replace("\r\n", "\n", $css);
                $search = array("/\/\*[^!][\d\D]*?\*\/|\t+/","/\s+/", "/\}\s+/");
                $replace = array(null," ", "}\n");
                $css = preg_replace($search, $replace, $css);
                $search = array("/;[\s+]/","/[\s+];/","/\s+\{\\s+/", "/\\:\s+\\#/", "/,\s+/i", "/\\:\s+\\\'/i","/\\:\s+([0-9]+|[A-F]+)/i","/\{\\s+/","/;}/");
                $replace = array(";",";","{", ":#", ",", ":\'", ":$1","{","}");
                $css = preg_replace($search, $replace, $css);
                $css = str_replace("\n", null, $css);
                return $css;    
            

            }


            http://mhameen.blogspot.com/2010/04/crystal-script-manger-for-php.html#links

            【讨论】:

              猜你喜欢
              • 2019-07-30
              • 2017-03-27
              • 2023-03-16
              • 1970-01-01
              • 1970-01-01
              • 2013-02-18
              • 2015-11-18
              • 2018-08-05
              • 2013-02-05
              相关资源
              最近更新 更多