【问题标题】:Minify a CSS file using PHP使用 PHP 缩小 CSS 文件
【发布时间】:2023-03-16 11:09:01
【问题描述】:

以下代码从 CSS 文件中删除所有换行符和空格。但问题是如果 CSS 文件有这样的东西:

.sample {
    padding: 0px 2px 1px 4px;
}

输出将是:

.sample{padding:0px2px1px4px;}

我想要介于 (0px 2px 1px 4px) 之间的空间。

这是我使用的代码:

$str=file_get_contents('sample.css');

//replace all new lines and spaces
$str = str_replace("\n", "", $str);
$str = str_replace(" ", "", $str);

//write the entire string
file_put_contents('sample.css', $str);

【问题讨论】:

  • 网络上不是已经有很多脚本可以做到这一点了吗?为什么要重新发明轮子?刚刚选择了第一个谷歌条目:github.com/matthiasmullie/minify
  • @Jeff :有时第一个选项不是最佳选择。我试过 Marrias Mullie 的 minify 库以及 MrClay 的同名库 (github.com/mrclay/minify),我更喜欢后者。此外,后一个库更受欢迎。只需比较两个库的观察者、星星和分叉的数量!

标签: php html css regex minify


【解决方案1】:

我写了这个小preg_replace

$css = preg_replace(
  array('/\s*(\w)\s*{\s*/','/\s*(\S*:)(\s*)([^;]*)(\s|\n)*;(\n|\s)*/','/\n/','/\s*}\s*/'), 
  array('$1{ ','$1$3;',"",'} '),
  $css
);

它会适当地折叠所有内容 - 在属性定义中留下空格,但不会删除 cmets。

【讨论】:

    【解决方案2】:

    这是一个简单而有效的解决方案:

    // Get style contents    
    $style = file_put_contents('your_style.css');
    
    // Minify it
    $minified = $style;
    $minified = str_replace("\n", "", $minified);
    $minified = str_replace("  ", " ", $minified);
    $minified = str_replace("  ", " ", $minified);
    $minified = str_replace(" {", "{", $minified);
    $minified = str_replace("{ ", "{", $minified);
    $minified = str_replace(" }", "}", $minified);
    $minified = str_replace("} ", "}", $minified);
    $minified = str_replace(", ", ",", $minified);
    $minified = str_replace("; ", ";", $minified);
    $minified = str_replace(": ", ":", $minified);
    
    // Write it
    header("Content-type: text/css; charset: UTF-8");
    echo $minified;
    

    它将删除所有不必要的空格。但是,它不会删除 cmets 或将 4 个数字替换为 1 可以替换它,但是它做得很好。 如果我遗漏了某些内容,或者您​​有任何想法可以轻松添加评论删除和/或其他功能,请告诉我。

    【讨论】:

      【解决方案3】:

      要在 PHP 中缩小 CSS,最好使用 Steve ClayMinify library。重新发明轮子是没有意义的。

      Here 简要介绍了如何安装和配置库。

      【讨论】:

        【解决方案4】:

        在您的代码中,添加以下行:

        $str = preg_replace("/([0-9]*px(?!;))/", "$1 ", $str);
        

        将在任何 px 字符串中添加一个空格,后跟不是 ;

        这样,您可以通过在您指出的位置添加空格来修复代码。

        $str=file_get_contents('sample.css');
        
        //replace all new lines and spaces
        $str = str_replace("\n", "", $str);
        $str = str_replace(" ", "", $str);
        $str = preg_replace("/([0-9]*px(?!;))/", "$1 ", $str);
        
        //write the entire string
        file_put_contents('sample.css', $str);
        

        您可以使用任何提供完整 css 压缩选项的 Php 压缩库,例如 minify

        我希望这会有所帮助。

        【讨论】:

          【解决方案5】:

          如果您想删除每行周围的制表符和空格,但保留样式内的空格。您应该只使用explode() 将整个内容以\n 作为标记分隔符并遍历每一行并在其上使用php 的trim(),然后在没有任何分隔符的情况下使用implode()

          【讨论】:

            猜你喜欢
            • 2012-10-23
            • 2012-10-13
            • 1970-01-01
            • 2013-05-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-10-05
            相关资源
            最近更新 更多