【问题标题】:Minify index.php缩小 index.php
【发布时间】:2016-04-16 01:52:08
【问题描述】:

情况

我正在努力缩小我的 index.php 文件


我试过了

这是我的内容:index.php

<?php include 'master.php'; ?>

<?php

function htmlmin($buffer)

{
    $search = array( '/\>[^\S ]+/s', '/[^\S ]+\</s', '/(\s)+/s' );
    $replace = array('>','<','\\1');

    if (preg_match("/\<html/i",$buffer) == 1 && preg_match("/\<\/html\>/i", $buffer) == 1) {
        $buffer = preg_replace($search, $replace, $buffer);
    }

    return $buffer;
}

ob_start("htmlmin");

?>

当我执行view page source 时,我仍然看到我的 html 输出没有缩小。 我知道我做错了什么,但我不确定是什么。

我忘记做某事了吗?我在正确的轨道上吗? 有人可以给我提示吗?

【问题讨论】:

  • 也许你看看那个链接:minify
  • @bub :谢谢,但我确实已经查看了那个链接,但我仍然无法让它工作。你有什么建议吗?
  • 嗯,我唯一的建议是:不要使用正则表达式做 HTML!!!
  • 我完全同意你的看法。我之所以这样尝试,是因为我没有别的办法。感谢您的建议。

标签: php html minify


【解决方案1】:

我解决了这个问题

通过使用 grunt 任务之一:grunt-contrib-htmlmin


安装

你可以用这个命令安装这个插件:

npm install grunt-contrib-htmlmin --save-dev

安装插件后,将其添加到您的 Gruntfile

grunt.loadNpmTasks('grunt-contrib-htmlmin');


设置

htmlmin: {

    dist: {
        files: {
            'index.php': 'index.php',
        }
    }
}

最终文件

grunt.initConfig({
    
        htmlmin: {

            dist: {
                files: {
                    'index.php': 'index.php',
                }
            }
        }

    });


    // Load NPM Tasks
    grunt.loadNpmTasks('grunt-contrib-htmlmin');

    // Default Setting
    grunt.registerTask('default', ['htmlmin']);

};

执行

只需在终端上运行grunt,index.php 就会被缩小。


测试/结果

如果我不向你们展示结果,那将不会很有趣。就是这样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-23
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 2011-08-12
    相关资源
    最近更新 更多