douchixu3686 2015-05-12 14:20
浏览 16

缩小index.php

Situation

I'm struggling trying to minify my index.php file


I've tried

Here is what's in my : 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");

?>

When I do view page source, I still see my html output is not minify. I know that I did something wrong, but I'm not sure what it is.

Did I forget to do something ? Am I on the right track ? Can someone please give me hint ?

  • 写回答

1条回答 默认 最新

  • dsiimyoc804955 2015-05-13 13:27
    关注

    I solved this

    by using one of the grunt task: grunt-contrib-htmlmin


    Install

    you may install this plugin with this command:

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

    Once the plugin has been installed, add this to your Gruntfile

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


    Setting

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

    Final File

    grunt.initConfig({
    
            htmlmin: {
    
                dist: {
                    files: {
                        'index.php': 'index.php',
                    }
                }
            }
    
        });
    
    
        // Load NPM Tasks
        grunt.loadNpmTasks('grunt-contrib-htmlmin');
    
        // Default Setting
        grunt.registerTask('default', ['htmlmin']);
    
    };
    

    Execute

    Just run grunt on your terminal, the index.php will be minified.


    Test/Result

    It will not be fun, If I don't show you guys the result. Here is it.

    enter image description here

    评论

报告相同问题?