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 Stable Version Download Development Version

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

^232 Downloads
v2.6.6116 Downloads
v2.6.554 Downloads
v2.6.455 Downloads
v2.5.788 Downloads
v2.5.666 Downloads
v2.5.4101 Downloads
v2.5.3113 Downloads
v2.5.299 Downloads
v2.5.1116 Downloads
v2.0.4113 Downloads

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' => [2]
]
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;
  color: #000;
  font: normal normal 16px/1.25 sans-serif;
}');

JS 

Minify::JS(string $in, int $comment = 2, int $quote = 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, int $comment = 2): 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.