douchixu3686 2015-05-12 06: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 05: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

    展开全部

    评论
    编辑
    预览

    报告相同问题?

    手机看
    程序员都在用的中文IT技术交流社区

    程序员都在用的中文IT技术交流社区

    专业的中文 IT 技术社区,与千万技术人共成长

    专业的中文 IT 技术社区,与千万技术人共成长

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    客服 返回
    顶部