1

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 ?

Community
  • 1
  • 1
code-8
  • 54,650
  • 106
  • 352
  • 604
  • Maybe you look into that link: [minify](http://stackoverflow.com/questions/6225351/how-to-minify-php-page-html-output) – B001ᛦ May 12 '15 at 14:32
  • @bub : Thanks, but I did take a look in that link already, and I still couldn't get it to work. Do you have any suggestions ? – code-8 May 12 '15 at 14:38
  • 3
    Well the only thing I suggest is: Do not do HTML using regular expressions!!! – B001ᛦ May 12 '15 at 14:41
  • I totally agree with you. The reason why I tried it that way because I don't what else to do. Thanks for your advise. – code-8 May 12 '15 at 14:43

1 Answers1

0

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

Community
  • 1
  • 1
code-8
  • 54,650
  • 106
  • 352
  • 604