Mecha CMS

Add extra functionality to the core engine.

Minify

Table of Contents
  1. Usage 
    1. HTML 
    2. CSS 
    3. JS 
    4. JSON 
    5. PHP 
    6. Options 

Remove white-spaces and unused comments in HTML, CSS and JavaScript files.

Download Version 2.5.1 Download Development Version

Something looks broken? We accept bounties to fix them faster.

This extension reduces the page size by removing unused white-spaces and comments in the HTML output.

// `Minify::{$any}($in, $comment, $quote)`
[
    '.css' => [2, 2], // `[$comment, $quote]`
    '.html' => [2, 1],
    '.js' => [2],
    '.json' => [],
    '.php' => []
]
CodeDescription
0Remove
1Keep
2Remove if/but/when…

Note: There is no such 0 option for $quote.


Usage 

HTML 

Minify::HTML(string $in, int $comment = 2, int $quote = 1): string;

Minify HTML string:

echo Minify::HTML('<!DOCTYPE html>
<html dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Test</title>
    <!-- remove -->
  </head>
  <body>
    <!--[if IE]> keep <![endif]-->
    <h1>Test</h1>
    <p>
      <button>Button 1</button> <button>Button 2</button>
    </p>
  </body>
</html>');

CSS 

Minify::CSS(string $in, int $comment = 2, int $quote = 2): string;

Minify CSS string:

echo Minify::CSS('* {
  margin: 0;
  padding: 0;
  font: inherit;
  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
          box-sizing: border-box;
}

/* remove */

/*! keep */

/** keep */

/* @licence keep */

/* @license keep */

/* @preserve keep */

html {
  background: #fff;
  font: normal normal 13px/1.5 Helmet, FreeSans, sans-serif;
  color: #000;
}');

JS 

Minify::JS(string $in, int $comment = 2): string;

Minify JavaScript string:

echo Minify::JS('var i, j, k = Date.now();

for (i = 0, j = k; i < k; ++i) {
    console.log("bark: " + i);
}');

JSON 

Minify::JSON(string $in): string;

PHP 

Minify::PHP(string $in): string;

Options 

ParameterDescription
$inThe string input to be minified.
$commentThe $comment parameter value consists of 0, 1 and 2. 0 means remove all comments, 1 means preserve all comments, 2 means remove all comments except special comments like IE conditional comments or license comments.
$quoteThe $quote parameter value consists of 1 and 2. 1 means preserve all quotes, 2 means remove quotes where possible.