Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Alliline/fc5e9827cefea447bb74390b7108c6b9 to your computer and use it in GitHub Desktop.

Select an option

Save Alliline/fc5e9827cefea447bb74390b7108c6b9 to your computer and use it in GitHub Desktop.

Revisions

  1. @Rodrigo54 Rodrigo54 revised this gist May 31, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion php-html-css-js-minifier.php
    Original file line number Diff line number Diff line change
    @@ -72,7 +72,7 @@ function minify_css($input) {
    // Remove comment(s)
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|\/\*(?!\!)(?>.*?\*\/)|^\s*|\s*$#s',
    // Remove unused white-space(s)
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/))|\s*+;\s*+(})\s*+|\s*+([*$~^|]?+=|[{};,>~+]|\s*+-(?![0-9\.])|!important\b)\s*+|([[(:])\s++|\s++([])])|\s++(:)\s*+(?!(?>[^{}"\']++|"(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')*+{)|^\s++|\s++\z|(\s)\s+#si',
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/))|\s*+;\s*+(})\s*+|\s*+([*$~^|]?+=|[{};,>~]|\s(?![0-9\.])|!important\b)\s*+|([[(:])\s++|\s++([])])|\s++(:)\s*+(?!(?>[^{}"\']++|"(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')*+{)|^\s++|\s++\z|(\s)\s+#si',
    // Replace `0(cm|em|ex|in|mm|pc|pt|px|vh|vw|%)` with `0`
    '#(?<=[\s:])(0)(cm|em|ex|in|mm|pc|pt|px|vh|vw|%)#si',
    // Replace `:0 0 0 0` with `:0`
  2. @Rodrigo54 Rodrigo54 revised this gist Feb 6, 2016. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions php-html-css-js-minifier.php
    Original file line number Diff line number Diff line change
    @@ -19,6 +19,17 @@ function minify_html($input) {
    return '<' . $matches[1] . ' style=' . $matches[2] . minify_css($matches[3]) . $matches[2];
    }, $input);
    }
    if(strpos($input, '</style>') !== false) {
    $input = preg_replace_callback('#<style(.*?)>(.*?)</style>#is', function($matches) {
    return '<style' . $matches[1] .'>'. minify_css($matches[2]) . '</style>';
    }, $input);
    }
    if(strpos($input, '</script>') !== false) {
    $input = preg_replace_callback('#<script(.*?)>(.*?)</script>#is', function($matches) {
    return '<script' . $matches[1] .'>'. minify_js($matches[2]) . '</script>';
    }, $input);
    }

    return preg_replace(
    array(
    // t = text
  3. @taufik-nurrohman taufik-nurrohman revised this gist Nov 22, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion php-html-css-js-minifier.php
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@ function minify_html($input) {
    '#(&nbsp;)&nbsp;(?![<\s])#', // clean up ...
    '#(?<=\>)(&nbsp;)(?=\<)#', // --ibid
    // Remove HTML comment(s) except IE comment(s)
    '#\s*<!--(?!\[if\s).*?-->\s*|(?<!\>)\n+(?=\<[^\/!])#s'
    '#\s*<!--(?!\[if\s).*?-->\s*|(?<!\>)\n+(?=\<[^!])#s'
    ),
    array(
    '<$1$2</$1>',
  4. @taufik-nurrohman taufik-nurrohman revised this gist Nov 21, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion php-html-css-js-minifier.php
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@ function minify_html($input) {
    '#(&nbsp;)&nbsp;(?![<\s])#', // clean up ...
    '#(?<=\>)(&nbsp;)(?=\<)#', // --ibid
    // Remove HTML comment(s) except IE comment(s)
    '#\s*<!--(?!\[if\s).*?-->\s*|(?<!\>)\n+(?=\<)#s'
    '#\s*<!--(?!\[if\s).*?-->\s*|(?<!\>)\n+(?=\<[^\/!])#s'
    ),
    array(
    '<$1$2</$1>',
  5. @taufik-nurrohman taufik-nurrohman revised this gist Nov 21, 2015. 1 changed file with 48 additions and 83 deletions.
    131 changes: 48 additions & 83 deletions php-html-css-js-minifier.php
    Original file line number Diff line number Diff line change
    @@ -9,101 +9,75 @@
    // HTML Minifier
    function minify_html($input) {
    if(trim($input) === "") return $input;
    // Remove extra white-spaces between HTML attributes
    // Remove extra white-space(s) between HTML attribute(s)
    $input = preg_replace_callback('#<([^\/\s<>!]+)(?:\s+([^<>]*?)\s*|\s*)(\/?)>#s', function($matches) {
    return '<' . $matches[1] . preg_replace('#([^\s=]+)(\=([\'"]?)(.*?)\3)?(\s+|$)#s', ' $1$2', $matches[2]) . $matches[3] . '>';
    }, $input);
    // Minify inline CSS declarations
    }, str_replace("\r", "", $input));
    // Minify inline CSS declaration(s)
    if(strpos($input, ' style=') !== false) {
    $input = preg_replace_callback('#\s+style=([\'"]?)(.*?)\1(?=[\/\s>])#s', function($matches) {
    return ' style=' . $matches[1] . minify_css($matches[2]) . $matches[1];
    $input = preg_replace_callback('#<([^<]+?)\s+style=([\'"])(.*?)\2(?=[\/\s>])#s', function($matches) {
    return '<' . $matches[1] . ' style=' . $matches[2] . minify_css($matches[3]) . $matches[2];
    }, $input);
    }
    return preg_replace(
    array(
    // Remove HTML comments except IE comments
    '#\s*(<\!--(?=\[if).*?-->)\s*|\s*<\!--.*?-->\s*#s',
    // Do not remove white-space after image and
    // input tag that is followed by a tag open
    '#(<(?:img|input)(?:\/?>|\s[^<>]*?\/?>))\s+(?=\<[^\/])#s',
    // Remove two or more white-spaces between tags
    '#(<\!--.*?-->)|(>)\s{2,}|\s{2,}(<)|(>)\s{2,}(<)#s',
    // Proofing ...
    // o: tag open, c: tag close, t: text
    // If `<tag> </tag>` remove white-space
    // If `</tag> <tag>` keep white-space
    // If `<tag> <tag>` remove white-space
    // If `</tag> </tag>` remove white-space
    // If `<tag> ...</tag>` remove white-spaces
    // If `</tag> ...<tag>` remove white-spaces
    // If `<tag> ...<tag>` remove white-spaces
    // If `</tag> ...</tag>` remove white-spaces
    // If `abc <tag>` keep white-space
    // If `<tag> abc` remove white-space
    // If `abc </tag>` remove white-space
    // If `</tag> abc` keep white-space
    // TODO: If `abc ...<tag>` keep one white-space
    // If `<tag> ...abc` remove white-spaces
    // If `abc ...</tag>` remove white-spaces
    // TODO: If `</tag> ...abc` keep one white-space
    '#(<\!--.*?-->)|(<(?:img|input)(?:\/?>|\s[^<>]*?\/?>))\s+(?!\<\/)#s', // o+t | o+o
    '#(<\!--.*?-->)|(<[^\/\s<>]+(?:>|\s[^<>]*?>))\s+(?=\<[^\/])#s', // o+o
    '#(<\!--.*?-->)|(<\/[^\/\s<>]+?>)\s+(?=\<\/)#s', // c+c
    '#(<\!--.*?-->)|(<([^\/\s<>]+)(?:>|\s[^<>]*?>))\s+(<\/\3>)#s', // o+c
    '#(<\!--.*?-->)|(<[^\/\s<>]+(?:>|\s[^<>]*?>))\s+(?!\<)#s', // o+t
    '#(<\!--.*?-->)|(?!\>)\s+(<\/[^\/\s<>]+?>)#s', // t+c
    '#(<\!--.*?-->)|(?!\>)\s+(?=\<[^\/])#s', // t+o
    '#(<\!--.*?-->)|(<\/[^\/\s<>]+?>)\s+(?!\<)#s', // c+t
    '#(<\!--.*?-->)|(\/>)\s+(?!\<)#', // o+t
    // Replace `&nbsp;&nbsp;&nbsp;` with `&nbsp; &nbsp;`
    '#(?<=&nbsp;)(&nbsp;){2}#',
    // Proofing ...
    '#(?<=\>)&nbsp;(?!\s|&nbsp;|<\/)#',
    '#(?<=--\>)(?:\s|&nbsp;)+(?=\<)#'
    // t = text
    // o = tag open
    // c = tag close
    // Keep important white-space(s) after self-closing HTML tag(s)
    '#<(img|input)(>| .*?>)#s',
    // Remove a line break and two or more white-space(s) between tag(s)
    '#(<!--.*?-->)|(>)(?:\n*|\s{2,})(<)|^\s*|\s*$#s',
    '#(<!--.*?-->)|(?<!\>)\s+(<\/.*?>)|(<[^\/]*?>)\s+(?!\<)#s', // t+c || o+t
    '#(<!--.*?-->)|(<[^\/]*?>)\s+(<[^\/]*?>)|(<\/.*?>)\s+(<\/.*?>)#s', // o+o || c+c
    '#(<!--.*?-->)|(<\/.*?>)\s+(\s)(?!\<)|(?<!\>)\s+(\s)(<[^\/]*?\/?>)|(<[^\/]*?\/?>)\s+(\s)(?!\<)#s', // c+t || t+o || o+t -- separated by long white-space(s)
    '#(<!--.*?-->)|(<[^\/]*?>)\s+(<\/.*?>)#s', // empty tag
    '#<(img|input)(>| .*?>)<\/\1>#s', // reset previous fix
    '#(&nbsp;)&nbsp;(?![<\s])#', // clean up ...
    '#(?<=\>)(&nbsp;)(?=\<)#', // --ibid
    // Remove HTML comment(s) except IE comment(s)
    '#\s*<!--(?!\[if\s).*?-->\s*|(?<!\>)\n+(?=\<)#s'
    ),
    array(
    '$1',
    '$1&nbsp;',
    '<$1$2</$1>',
    '$1$2$3',
    '$1$2$3',
    '$1$2$3$4$5',
    '$1$2&nbsp;', // o+t | o+o
    '$1$2', // o+o
    '$1$2', //c+c
    '$1$2$4', // o+c
    '$1$2', // o+t
    '$1$2', // t+c
    '$1$2 ', // t+o
    '$1$2 ', // c+t
    '$1$2 ', // o+t
    ' $1',
    ' ',
    '$1$2$3$4$5$6$7',
    '$1$2$3',
    '<$1$2',
    '$1 ',
    '$1',
    ""
    ),
    trim($input));
    $input);
    }

    // CSS Minifier => http://ideone.com/Q5USEF + improvement(s)
    function minify_css($input) {
    if(trim($input) === "") return $input;
    return preg_replace(
    array(
    // Remove comments
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|\/\*(?!\!)(?>.*?\*\/)#s',
    // Remove unused white-spaces
    // Remove comment(s)
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|\/\*(?!\!)(?>.*?\*\/)|^\s*|\s*$#s',
    // Remove unused white-space(s)
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/))|\s*+;\s*+(})\s*+|\s*+([*$~^|]?+=|[{};,>~+]|\s*+-(?![0-9\.])|!important\b)\s*+|([[(:])\s++|\s++([])])|\s++(:)\s*+(?!(?>[^{}"\']++|"(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')*+{)|^\s++|\s++\z|(\s)\s+#si',
    // Replace `0(cm|em|ex|in|mm|pc|pt|px|vh|vw|%)` with `0`
    '#(?<=[:\s])(0)(cm|em|ex|in|mm|pc|pt|px|vh|vw|%)#si',
    '#(?<=[\s:])(0)(cm|em|ex|in|mm|pc|pt|px|vh|vw|%)#si',
    // Replace `:0 0 0 0` with `:0`
    '#:(0\s+0|0\s+0\s+0\s+0)(?=[;\}]|\!important)#i',
    // Replace `background-position:0` with `background-position:0 0`
    '#(background-position):0(?=[;\}])#si',
    // Replace `0.6` with `.6`, but only when preceded by `:`, `-`, `,` or a white-space
    '#(?<=[:\-,\s])0+\.(\d+)#s',
    // Replace `0.6` with `.6`, but only when preceded by `:`, `,`, `-` or a white-space
    '#(?<=[\s:,\-])0+\.(\d+)#s',
    // Minify string value
    '#(\/\*(?>.*?\*\/))|(?<!content\:)([\'"])([a-z_][a-z0-9\-_]*?)\2(?=[\s\{\}\];,])#si',
    '#(\/\*(?>.*?\*\/))|(\burl\()([\'"])([^\s]+?)\3(\))#si',
    // Minify HEX color code
    '#(?<=[:\-,\s]\#)([a-f0-6]+)\1([a-f0-6]+)\2([a-f0-6]+)\3#i',
    // Remove empty selectors
    '#(?<=[\s:,\-]\#)([a-f0-6]+)\1([a-f0-6]+)\2([a-f0-6]+)\3#i',
    // Replace `(border|outline):none` with `(border|outline):0`
    '#(?<=[\{;])(border|outline):none(?=[;\}\!])#',
    // Remove empty selector(s)
    '#(\/\*(?>.*?\*\/))|(^|[\{\}])(?:[^\s\{\}]+)\{\}#s'
    ),
    array(
    @@ -116,43 +90,34 @@ function minify_css($input) {
    '$1$3',
    '$1$2$4$5',
    '$1$2$3',
    '$1:0',
    '$1$2'
    ),
    trim($input));
    $input);
    }

    // JavaScript Minifier
    function minify_js($input) {
    if(trim($input) === "") return $input;
    return preg_replace(
    array(
    // '#(?<!\\\)\\\\\"#',
    // '#(?<!\\\)\\\\\'#',
    // Remove comments
    '#\s*("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')\s*|\s*\/\*\s*(?!\!|@cc_on)(?>[\s\S]*?\*\/)\s*|\s*(?<![\:\=])\/\/.*[\n\r]*#',
    // Remove unused white-space characters outside the string and regex
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/)|\/(?!\/)[^\n\r]*?\/(?=[\.,;]|[gimuy]))|\s*([+\-\=\/%\(\)\{\}\[\]<>\|&\?\!\:;\.,])\s*#s',
    // Remove comment(s)
    '#\s*("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')\s*|\s*\/\*(?!\!|@cc_on)(?>[\s\S]*?\*\/)\s*|\s*(?<![\:\=])\/\/.*(?=[\n\r]|$)|^\s*|\s*$#',
    // Remove white-space(s) outside the string and regex
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/)|\/(?!\/)[^\n\r]*?\/(?=[\s.,;]|[gimuy]|$))|\s*([!%&*\(\)\-=+\[\]\{\}|;:,.<>?\/])\s*#s',
    // Remove the last semicolon
    '#;+\}#',
    // Replace `true` with `!0`
    // '#\btrue\b#',
    // Replace `false` with `!1`
    // '#\bfalse\b#',
    // Minify object attribute except JSON attribute. From `{'foo':'bar'}` to `{foo:'bar'}`
    // Minify object attribute(s) except JSON attribute(s). From `{'foo':'bar'}` to `{foo:'bar'}`
    '#([\{,])([\'])(\d+|[a-z_][a-z0-9_]*)\2(?=\:)#i',
    // --ibid. From `foo['bar']` to `foo.bar`
    '#([a-z0-9_\)\]])\[([\'"])([a-z_][a-z0-9_]*)\2\]#i'
    ),
    array(
    // '\\u0022',
    // '\\u0027',
    '$1',
    '$1$2',
    '}',
    // '!0',
    // '!1',
    '$1$3',
    '$1.$3'
    ),
    trim($input));
    $input);
    }
  6. @taufik-nurrohman taufik-nurrohman revised this gist Apr 13, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion php-html-css-js-minifier.php
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ function minify_html($input) {
    }, $input);
    // Minify inline CSS declarations
    if(strpos($input, ' style=') !== false) {
    $input = preg_replace_callback('#\s+style=([\'"]?)(.*?)\1#s', function($matches) {
    $input = preg_replace_callback('#\s+style=([\'"]?)(.*?)\1(?=[\/\s>])#s', function($matches) {
    return ' style=' . $matches[1] . minify_css($matches[2]) . $matches[1];
    }, $input);
    }
  7. @taufik-nurrohman taufik-nurrohman revised this gist Apr 13, 2015. 1 changed file with 8 additions and 10 deletions.
    18 changes: 8 additions & 10 deletions php-html-css-js-minifier.php
    Original file line number Diff line number Diff line change
    @@ -42,12 +42,10 @@ function minify_html($input) {
    // If `<tag> abc` remove white-space
    // If `abc </tag>` remove white-space
    // If `</tag> abc` keep white-space
    // If `abc ...<tag>` keep one white-space
    // TODO: If `abc ...<tag>` keep one white-space
    // If `<tag> ...abc` remove white-spaces
    // If `abc ...</tag>` remove white-spaces
    // If `</tag> ...abc` keep one white-space
    // If `abc ...<tag>` keep one white-space
    // If `abc ...</tag>` remove white-spaces
    // TODO: If `</tag> ...abc` keep one white-space
    '#(<\!--.*?-->)|(<(?:img|input)(?:\/?>|\s[^<>]*?\/?>))\s+(?!\<\/)#s', // o+t | o+o
    '#(<\!--.*?-->)|(<[^\/\s<>]+(?:>|\s[^<>]*?>))\s+(?=\<[^\/])#s', // o+o
    '#(<\!--.*?-->)|(<\/[^\/\s<>]+?>)\s+(?=\<\/)#s', // c+c
    @@ -60,7 +58,7 @@ function minify_html($input) {
    // Replace `&nbsp;&nbsp;&nbsp;` with `&nbsp; &nbsp;`
    '#(?<=&nbsp;)(&nbsp;){2}#',
    // Proofing ...
    '#(?<=\>)&nbsp;(?!\s|&nbsp;)#',
    '#(?<=\>)&nbsp;(?!\s|&nbsp;|<\/)#',
    '#(?<=--\>)(?:\s|&nbsp;)+(?=\<)#'
    ),
    array(
    @@ -93,11 +91,11 @@ function minify_css($input) {
    // Remove unused white-spaces
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/))|\s*+;\s*+(})\s*+|\s*+([*$~^|]?+=|[{};,>~+]|\s*+-(?![0-9\.])|!important\b)\s*+|([[(:])\s++|\s++([])])|\s++(:)\s*+(?!(?>[^{}"\']++|"(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')*+{)|^\s++|\s++\z|(\s)\s+#si',
    // Replace `0(cm|em|ex|in|mm|pc|pt|px|vh|vw|%)` with `0`
    '#(?<=[:\s])(0)(cm|em|ex|in|mm|pc|pt|px|vh|vw|%)#s',
    '#(?<=[:\s])(0)(cm|em|ex|in|mm|pc|pt|px|vh|vw|%)#si',
    // Replace `:0 0 0 0` with `:0`
    '#:0 0 0 0(?=[;\}]|\!important)#s',
    '#:(0\s+0|0\s+0\s+0\s+0)(?=[;\}]|\!important)#i',
    // Replace `background-position:0` with `background-position:0 0`
    '#background-position:0(?=[;\}])#s',
    '#(background-position):0(?=[;\}])#si',
    // Replace `0.6` with `.6`, but only when preceded by `:`, `-`, `,` or a white-space
    '#(?<=[:\-,\s])0+\.(\d+)#s',
    // Minify string value
    @@ -106,14 +104,14 @@ function minify_css($input) {
    // Minify HEX color code
    '#(?<=[:\-,\s]\#)([a-f0-6]+)\1([a-f0-6]+)\2([a-f0-6]+)\3#i',
    // Remove empty selectors
    '#(\/\*(?>.*?\*\/))|(^|[\{\}])(?:[^\s\{\}]+)\{\}#si'
    '#(\/\*(?>.*?\*\/))|(^|[\{\}])(?:[^\s\{\}]+)\{\}#s'
    ),
    array(
    '$1',
    '$1$2$3$4$5$6$7',
    '$1',
    ':0',
    'background-position:0 0',
    '$1:0 0',
    '.$1',
    '$1$3',
    '$1$2$4$5',
  8. @taufik-nurrohman taufik-nurrohman revised this gist Apr 11, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion php-html-css-js-minifier.php
    Original file line number Diff line number Diff line change
    @@ -58,7 +58,7 @@ function minify_html($input) {
    '#(<\!--.*?-->)|(<\/[^\/\s<>]+?>)\s+(?!\<)#s', // c+t
    '#(<\!--.*?-->)|(\/>)\s+(?!\<)#', // o+t
    // Replace `&nbsp;&nbsp;&nbsp;` with `&nbsp; &nbsp;`
    '#(?<=&nbsp;)(&nbsp;){2}#s',
    '#(?<=&nbsp;)(&nbsp;){2}#',
    // Proofing ...
    '#(?<=\>)&nbsp;(?!\s|&nbsp;)#',
    '#(?<=--\>)(?:\s|&nbsp;)+(?=\<)#'
  9. @taufik-nurrohman taufik-nurrohman revised this gist Apr 11, 2015. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions php-html-css-js-minifier.php
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ function minify_html($input) {
    }, $input);
    // Minify inline CSS declarations
    if(strpos($input, ' style=') !== false) {
    $input = preg_replace_callback('#\s+style=([\'"]?)(.*?)\1#', function($matches) {
    $input = preg_replace_callback('#\s+style=([\'"]?)(.*?)\1#s', function($matches) {
    return ' style=' . $matches[1] . minify_css($matches[2]) . $matches[1];
    }, $input);
    }
    @@ -104,7 +104,7 @@ function minify_css($input) {
    '#(\/\*(?>.*?\*\/))|(?<!content\:)([\'"])([a-z_][a-z0-9\-_]*?)\2(?=[\s\{\}\];,])#si',
    '#(\/\*(?>.*?\*\/))|(\burl\()([\'"])([^\s]+?)\3(\))#si',
    // Minify HEX color code
    '#(?<=[:\-,\s]\#)([a-z0-6]+)\1([a-z0-6]+)\2([a-z0-6]+)\3#i',
    '#(?<=[:\-,\s]\#)([a-f0-6]+)\1([a-f0-6]+)\2([a-f0-6]+)\3#i',
    // Remove empty selectors
    '#(\/\*(?>.*?\*\/))|(^|[\{\}])(?:[^\s\{\}]+)\{\}#si'
    ),
    @@ -131,9 +131,9 @@ function minify_js($input) {
    // '#(?<!\\\)\\\\\"#',
    // '#(?<!\\\)\\\\\'#',
    // Remove comments
    '#\s*("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')\s*|\s*\/\*\s*(?!\!|@cc_on)(?>[\s\S]*?\*\/)\s*|\s*(?<![\:\=])\/\/.*[\n]*#',
    '#\s*("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')\s*|\s*\/\*\s*(?!\!|@cc_on)(?>[\s\S]*?\*\/)\s*|\s*(?<![\:\=])\/\/.*[\n\r]*#',
    // Remove unused white-space characters outside the string and regex
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/)|\/(?!\/)[^\n]*?\/(?=[\.,;]|[gimuy]))|\s*([+\-\=\/%\(\)\{\}\[\]<>\|&\?\!\:;\.,])\s*#s',
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/)|\/(?!\/)[^\n\r]*?\/(?=[\.,;]|[gimuy]))|\s*([+\-\=\/%\(\)\{\}\[\]<>\|&\?\!\:;\.,])\s*#s',
    // Remove the last semicolon
    '#;+\}#',
    // Replace `true` with `!0`
  10. @taufik-nurrohman taufik-nurrohman revised this gist Apr 11, 2015. 1 changed file with 19 additions and 10 deletions.
    29 changes: 19 additions & 10 deletions php-html-css-js-minifier.php
    Original file line number Diff line number Diff line change
    @@ -10,18 +10,24 @@
    function minify_html($input) {
    if(trim($input) === "") return $input;
    // Remove extra white-spaces between HTML attributes
    $input = preg_replace_callback('#<([^\/\s<>!]+?)(?:\s+([^<>]*?)\s*|\s*)(\/?)>#s', function($matches) {
    return '<' . $matches[1] . preg_replace('#([^\s\=\'"]*?\=([\'"]?).*?\2)(\s+|$)#s', ' $1', $matches[2]) . $matches[3] . '>';
    $input = preg_replace_callback('#<([^\/\s<>!]+)(?:\s+([^<>]*?)\s*|\s*)(\/?)>#s', function($matches) {
    return '<' . $matches[1] . preg_replace('#([^\s=]+)(\=([\'"]?)(.*?)\3)?(\s+|$)#s', ' $1$2', $matches[2]) . $matches[3] . '>';
    }, $input);
    // Minify inline CSS declarations
    if(strpos($input, ' style=') !== false) {
    $input = preg_replace_callback('#\s+style=([\'"]?)(.*?)\1#', function($matches) {
    return ' style=' . $matches[1] . minify_css($matches[2]) . $matches[1];
    }, $input);
    }
    return preg_replace(
    array(
    // Remove HTML comments except IE comments
    '#\s*(<\!--(?=\[if).*?-->)\s*|\s*<\!--.*?-->\s*#s',
    // Do not remove white-space after image and
    // input tag that is followed by a tag open
    '#(<(?:img|input)(?:\s[^<>]*?>|>))\s+(?=\<[^\/])#',
    '#(<(?:img|input)(?:\/?>|\s[^<>]*?\/?>))\s+(?=\<[^\/])#s',
    // Remove two or more white-spaces between tags
    '#(<\!--.*?-->)|(>)[^\S ]{2,}|[^\S ]{2,}(<)|(>)\s{2,}(<)#s',
    '#(<\!--.*?-->)|(>)\s{2,}|\s{2,}(<)|(>)\s{2,}(<)#s',
    // Proofing ...
    // o: tag open, c: tag close, t: text
    // If `<tag> </tag>` remove white-space
    @@ -42,11 +48,11 @@ function minify_html($input) {
    // If `</tag> ...abc` keep one white-space
    // If `abc ...<tag>` keep one white-space
    // If `abc ...</tag>` remove white-spaces
    '#(<\!--.*?-->)|(<(?:img|input)(?:\s[^<>]*?>|>))\s+(?!\<\/)#s', // o+t | o+o
    '#(<\!--.*?-->)|(<[^\/\s<>]+(?:\s[^<>]*?>|>))\s+(?=\<[^\/])#s', // o+o
    '#(<\!--.*?-->)|(<(?:img|input)(?:\/?>|\s[^<>]*?\/?>))\s+(?!\<\/)#s', // o+t | o+o
    '#(<\!--.*?-->)|(<[^\/\s<>]+(?:>|\s[^<>]*?>))\s+(?=\<[^\/])#s', // o+o
    '#(<\!--.*?-->)|(<\/[^\/\s<>]+?>)\s+(?=\<\/)#s', // c+c
    '#(<\!--.*?-->)|(<([^\/\s<>]+)(?:\s[^<>]*?>|>))\s+(<\/\3>)#s', // o+c
    '#(<\!--.*?-->)|(<[^\/\s<>]+(?:\s[^<>]*?>|>))\s+(?!\<)#s', // o+t
    '#(<\!--.*?-->)|(<([^\/\s<>]+)(?:>|\s[^<>]*?>))\s+(<\/\3>)#s', // o+c
    '#(<\!--.*?-->)|(<[^\/\s<>]+(?:>|\s[^<>]*?>))\s+(?!\<)#s', // o+t
    '#(<\!--.*?-->)|(?!\>)\s+(<\/[^\/\s<>]+?>)#s', // t+c
    '#(<\!--.*?-->)|(?!\>)\s+(?=\<[^\/])#s', // t+o
    '#(<\!--.*?-->)|(<\/[^\/\s<>]+?>)\s+(?!\<)#s', // c+t
    @@ -97,6 +103,8 @@ function minify_css($input) {
    // Minify string value
    '#(\/\*(?>.*?\*\/))|(?<!content\:)([\'"])([a-z_][a-z0-9\-_]*?)\2(?=[\s\{\}\];,])#si',
    '#(\/\*(?>.*?\*\/))|(\burl\()([\'"])([^\s]+?)\3(\))#si',
    // Minify HEX color code
    '#(?<=[:\-,\s]\#)([a-z0-6]+)\1([a-z0-6]+)\2([a-z0-6]+)\3#i',
    // Remove empty selectors
    '#(\/\*(?>.*?\*\/))|(^|[\{\}])(?:[^\s\{\}]+)\{\}#si'
    ),
    @@ -109,6 +117,7 @@ function minify_css($input) {
    '.$1',
    '$1$3',
    '$1$2$4$5',
    '$1$2$3',
    '$1$2'
    ),
    trim($input));
    @@ -122,9 +131,9 @@ function minify_js($input) {
    // '#(?<!\\\)\\\\\"#',
    // '#(?<!\\\)\\\\\'#',
    // Remove comments
    '#\s*("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')\s*|\s*\/\*\s*(?!\!|@cc_on)(?>[\s\S]*?\*\/)\s*|\s*(?<![\:\=])\/\/.*[\n\r]*#',
    '#\s*("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')\s*|\s*\/\*\s*(?!\!|@cc_on)(?>[\s\S]*?\*\/)\s*|\s*(?<![\:\=])\/\/.*[\n]*#',
    // Remove unused white-space characters outside the string and regex
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/)|\/(?!\/)[^\n\r]*?\/(?=[\.,;]|[gimuy]))|\s*([+\-\=\/%\(\)\{\}\[\]<>\|&\?\!\:;\.,])\s*#s',
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/)|\/(?!\/)[^\n]*?\/(?=[\.,;]|[gimuy]))|\s*([+\-\=\/%\(\)\{\}\[\]<>\|&\?\!\:;\.,])\s*#s',
    // Remove the last semicolon
    '#;+\}#',
    // Replace `true` with `!0`
  11. @taufik-nurrohman taufik-nurrohman revised this gist Apr 8, 2015. 1 changed file with 117 additions and 26 deletions.
    143 changes: 117 additions & 26 deletions php-html-css-js-minifier.php
    Original file line number Diff line number Diff line change
    @@ -1,60 +1,151 @@
    <?php

    /**
    * -----------------------------------------------------------------------------------------
    * Based on `https://github.com/mecha-cms/mecha-cms/blob/master/system/kernel/converter.php`
    * -----------------------------------------------------------------------------------------
    */

    // HTML Minifier
    function minify_html($input) {
    if(trim($input) === "") return $input;
    // Remove extra white-spaces between HTML attributes
    $input = preg_replace_callback('#<([^\/\s<>!]+?)(?:\s+([^<>]*?)\s*|\s*)(\/?)>#s', function($matches) {
    return '<' . $matches[1] . preg_replace('#([^\s\=\'"]*?\=([\'"]?).*?\2)(\s+|$)#s', ' $1', $matches[2]) . $matches[3] . '>';
    }, $input);
    return preg_replace(
    array(
    '#<\!--(?!\[if)([\s\S]+?)-->#s', // Remove HTML comments except IE comments
    '#>[^\S ]+#s',
    '#[^\S ]+<#s',
    '#>\s{2,}<#s'
    // Remove HTML comments except IE comments
    '#\s*(<\!--(?=\[if).*?-->)\s*|\s*<\!--.*?-->\s*#s',
    // Do not remove white-space after image and
    // input tag that is followed by a tag open
    '#(<(?:img|input)(?:\s[^<>]*?>|>))\s+(?=\<[^\/])#',
    // Remove two or more white-spaces between tags
    '#(<\!--.*?-->)|(>)[^\S ]{2,}|[^\S ]{2,}(<)|(>)\s{2,}(<)#s',
    // Proofing ...
    // o: tag open, c: tag close, t: text
    // If `<tag> </tag>` remove white-space
    // If `</tag> <tag>` keep white-space
    // If `<tag> <tag>` remove white-space
    // If `</tag> </tag>` remove white-space
    // If `<tag> ...</tag>` remove white-spaces
    // If `</tag> ...<tag>` remove white-spaces
    // If `<tag> ...<tag>` remove white-spaces
    // If `</tag> ...</tag>` remove white-spaces
    // If `abc <tag>` keep white-space
    // If `<tag> abc` remove white-space
    // If `abc </tag>` remove white-space
    // If `</tag> abc` keep white-space
    // If `abc ...<tag>` keep one white-space
    // If `<tag> ...abc` remove white-spaces
    // If `abc ...</tag>` remove white-spaces
    // If `</tag> ...abc` keep one white-space
    // If `abc ...<tag>` keep one white-space
    // If `abc ...</tag>` remove white-spaces
    '#(<\!--.*?-->)|(<(?:img|input)(?:\s[^<>]*?>|>))\s+(?!\<\/)#s', // o+t | o+o
    '#(<\!--.*?-->)|(<[^\/\s<>]+(?:\s[^<>]*?>|>))\s+(?=\<[^\/])#s', // o+o
    '#(<\!--.*?-->)|(<\/[^\/\s<>]+?>)\s+(?=\<\/)#s', // c+c
    '#(<\!--.*?-->)|(<([^\/\s<>]+)(?:\s[^<>]*?>|>))\s+(<\/\3>)#s', // o+c
    '#(<\!--.*?-->)|(<[^\/\s<>]+(?:\s[^<>]*?>|>))\s+(?!\<)#s', // o+t
    '#(<\!--.*?-->)|(?!\>)\s+(<\/[^\/\s<>]+?>)#s', // t+c
    '#(<\!--.*?-->)|(?!\>)\s+(?=\<[^\/])#s', // t+o
    '#(<\!--.*?-->)|(<\/[^\/\s<>]+?>)\s+(?!\<)#s', // c+t
    '#(<\!--.*?-->)|(\/>)\s+(?!\<)#', // o+t
    // Replace `&nbsp;&nbsp;&nbsp;` with `&nbsp; &nbsp;`
    '#(?<=&nbsp;)(&nbsp;){2}#s',
    // Proofing ...
    '#(?<=\>)&nbsp;(?!\s|&nbsp;)#',
    '#(?<=--\>)(?:\s|&nbsp;)+(?=\<)#'
    ),
    array(
    "",
    '>',
    '<',
    '><'
    '$1',
    '$1&nbsp;',
    '$1$2$3$4$5',
    '$1$2&nbsp;', // o+t | o+o
    '$1$2', // o+o
    '$1$2', //c+c
    '$1$2$4', // o+c
    '$1$2', // o+t
    '$1$2', // t+c
    '$1$2 ', // t+o
    '$1$2 ', // c+t
    '$1$2 ', // o+t
    ' $1',
    ' ',
    ""
    ),
    $input);
    trim($input));
    }

    // CSS Minifier => http://ideone.com/Q5USEF + improvement(s)
    function minify_css($input) {
    if(trim($input) === "") return $input;
    return preg_replace(
    array(
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|/\*(?>.*?\*/)#s', // Remove comments
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|\s*+;\s*+(})\s*+|\s*+([*$~^|]?+=|[{};,>~+]|\s*+-(?![0-9\.])|!important\b)\s*+|([[(:])\s++|\s++([])])|\s++(:)\s*+(?!(?>[^{}"\']++|"(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')*+{)|^\s++|\s++\z|(\s)\s+#si',
    '#([\s:])(0)(cm|em|ex|in|mm|pc|pt|px|%)#', // Replace `0(cm|em|ex|in|mm|pc|pt|px|%)` with `0`
    '#:0 0 0 0([;\}])#', // Replace `:0 0 0 0` with `:0`
    '#background-position:0([;\}])#', // Replace `background-position:0` with `background-position:0 0`
    '#([\s:])0+\.(\d+)#' // Replace `0.6` to `.6`, but only when preceded by `:` or a white-space
    // Remove comments
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|\/\*(?!\!)(?>.*?\*\/)#s',
    // Remove unused white-spaces
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/))|\s*+;\s*+(})\s*+|\s*+([*$~^|]?+=|[{};,>~+]|\s*+-(?![0-9\.])|!important\b)\s*+|([[(:])\s++|\s++([])])|\s++(:)\s*+(?!(?>[^{}"\']++|"(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')*+{)|^\s++|\s++\z|(\s)\s+#si',
    // Replace `0(cm|em|ex|in|mm|pc|pt|px|vh|vw|%)` with `0`
    '#(?<=[:\s])(0)(cm|em|ex|in|mm|pc|pt|px|vh|vw|%)#s',
    // Replace `:0 0 0 0` with `:0`
    '#:0 0 0 0(?=[;\}]|\!important)#s',
    // Replace `background-position:0` with `background-position:0 0`
    '#background-position:0(?=[;\}])#s',
    // Replace `0.6` with `.6`, but only when preceded by `:`, `-`, `,` or a white-space
    '#(?<=[:\-,\s])0+\.(\d+)#s',
    // Minify string value
    '#(\/\*(?>.*?\*\/))|(?<!content\:)([\'"])([a-z_][a-z0-9\-_]*?)\2(?=[\s\{\}\];,])#si',
    '#(\/\*(?>.*?\*\/))|(\burl\()([\'"])([^\s]+?)\3(\))#si',
    // Remove empty selectors
    '#(\/\*(?>.*?\*\/))|(^|[\{\}])(?:[^\s\{\}]+)\{\}#si'
    ),
    array(
    '$1',
    '$1$2$3$4$5$6$7',
    '$1$2',
    ':0$1',
    'background-position:0 0$1',
    '$1.$2'
    '$1',
    ':0',
    'background-position:0 0',
    '.$1',
    '$1$3',
    '$1$2$4$5',
    '$1$2'
    ),
    $input);
    trim($input));
    }

    // JavaScript Minifier
    function minify_js($input) {
    if(trim($input) === "") return $input;
    return preg_replace(
    array(
    '#\/\*([\s\S]*?)\*\/|(?<!:)\/\/.*([\n\r]+|$)#', // Remove comments
    '#(?|\s*(".*?"|\'.*?\'|(?<=[\(=\s])\/.*?\/[gimuy]*(?=[.,;\s]))\s*|\s*([+-=\/%(){}\[\]<>|&?!:;.,])\s*)#s', // Remove unused white-space characters outside the string and regex
    '#;+\}#' // Remove the last semicolon
    // '#(?<!\\\)\\\\\"#',
    // '#(?<!\\\)\\\\\'#',
    // Remove comments
    '#\s*("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')\s*|\s*\/\*\s*(?!\!|@cc_on)(?>[\s\S]*?\*\/)\s*|\s*(?<![\:\=])\/\/.*[\n\r]*#',
    // Remove unused white-space characters outside the string and regex
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/)|\/(?!\/)[^\n\r]*?\/(?=[\.,;]|[gimuy]))|\s*([+\-\=\/%\(\)\{\}\[\]<>\|&\?\!\:;\.,])\s*#s',
    // Remove the last semicolon
    '#;+\}#',
    // Replace `true` with `!0`
    // '#\btrue\b#',
    // Replace `false` with `!1`
    // '#\bfalse\b#',
    // Minify object attribute except JSON attribute. From `{'foo':'bar'}` to `{foo:'bar'}`
    '#([\{,])([\'])(\d+|[a-z_][a-z0-9_]*)\2(?=\:)#i',
    // --ibid. From `foo['bar']` to `foo.bar`
    '#([a-z0-9_\)\]])\[([\'"])([a-z_][a-z0-9_]*)\2\]#i'
    ),
    array(
    "",
    // '\\u0022',
    // '\\u0027',
    '$1',
    '}'
    '$1$2',
    '}',
    // '!0',
    // '!1',
    '$1$3',
    '$1.$3'
    ),
    $input);
    trim($input));
    }
  12. @taufik-nurrohman taufik-nurrohman revised this gist Feb 11, 2015. 1 changed file with 6 additions and 12 deletions.
    18 changes: 6 additions & 12 deletions php-html-css-js-minifier.php
    Original file line number Diff line number Diff line change
    @@ -26,10 +26,10 @@ function minify_css($input) {
    array(
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|/\*(?>.*?\*/)#s', // Remove comments
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|\s*+;\s*+(})\s*+|\s*+([*$~^|]?+=|[{};,>~+]|\s*+-(?![0-9\.])|!important\b)\s*+|([[(:])\s++|\s++([])])|\s++(:)\s*+(?!(?>[^{}"\']++|"(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')*+{)|^\s++|\s++\z|(\s)\s+#si',
    '#([\s:])(0)(px|em|%|in|cm|mm|pc|pt|ex)#', // Replace `0(px|em|%|in|cm|mm|pc|pt|ex)` with `0`
    '#([\s:])(0)(cm|em|ex|in|mm|pc|pt|px|%)#', // Replace `0(cm|em|ex|in|mm|pc|pt|px|%)` with `0`
    '#:0 0 0 0([;\}])#', // Replace `:0 0 0 0` with `:0`
    '#background-position:0([;\}])#', // Replace `background-position:0` with `background-position:0 0`
    '#(:|\s)0+\\.(\d+)#' // Replace `0.6` to `.6`, but only when preceded by `:` or a white-space
    '#([\s:])0+\.(\d+)#' // Replace `0.6` to `.6`, but only when preceded by `:` or a white-space
    ),
    array(
    '$1',
    @@ -47,20 +47,14 @@ function minify_js($input) {
    if(trim($input) === "") return $input;
    return preg_replace(
    array(
    '#\/\*([\s\S]+?)\*\/|(?<!:)\/\/.*([\n\r]+|$)#', // Remove comments
    '#(^|[\n\r])\s*#', // Remove space and new-line characters at the beginning of line
    '#(?|\s*(".*?"|\'.*?\'|(?<=[\(=\s])\/.*?\/[gimuy]*(?=[.,;\s]))\s*|\s*([+-=\/%(){}\[\]<>|&?!:;,])\s*)#s', // Remove unused space characters outside the string and regex
    '#;\}#', // Remove the last semicolon
    '#$#',
    '#;+$#'
    '#\/\*([\s\S]*?)\*\/|(?<!:)\/\/.*([\n\r]+|$)#', // Remove comments
    '#(?|\s*(".*?"|\'.*?\'|(?<=[\(=\s])\/.*?\/[gimuy]*(?=[.,;\s]))\s*|\s*([+-=\/%(){}\[\]<>|&?!:;.,])\s*)#s', // Remove unused white-space characters outside the string and regex
    '#;+\}#' // Remove the last semicolon
    ),
    array(
    "",
    "",
    '$1',
    '}',
    ';',
    ';'
    '}'
    ),
    $input);
    }
  13. @taufik-nurrohman taufik-nurrohman revised this gist Jan 19, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion php-html-css-js-minifier.php
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ function minify_css($input) {
    return preg_replace(
    array(
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|/\*(?>.*?\*/)#s', // Remove comments
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|\s*+;\s*+(})\s*+|\s*+([*$~^|]?+=|[{};,>~+]|\s*+-(?!\d)|!important\b)\s*+|([[(:])\s++|\s++([])])|\s++(:)\s*+(?!(?>[^{}"\']++|"(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')*+{)|^\s++|\s++\z|(\s)\s+#si',
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|\s*+;\s*+(})\s*+|\s*+([*$~^|]?+=|[{};,>~+]|\s*+-(?![0-9\.])|!important\b)\s*+|([[(:])\s++|\s++([])])|\s++(:)\s*+(?!(?>[^{}"\']++|"(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')*+{)|^\s++|\s++\z|(\s)\s+#si',
    '#([\s:])(0)(px|em|%|in|cm|mm|pc|pt|ex)#', // Replace `0(px|em|%|in|cm|mm|pc|pt|ex)` with `0`
    '#:0 0 0 0([;\}])#', // Replace `:0 0 0 0` with `:0`
    '#background-position:0([;\}])#', // Replace `background-position:0` with `background-position:0 0`
  14. @taufik-nurrohman taufik-nurrohman revised this gist Jan 18, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion php-html-css-js-minifier.php
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,7 @@ function minify_js($input) {
    array(
    '#\/\*([\s\S]+?)\*\/|(?<!:)\/\/.*([\n\r]+|$)#', // Remove comments
    '#(^|[\n\r])\s*#', // Remove space and new-line characters at the beginning of line
    '#(?| *(".*?"|\'.*?\'|(?<=[\(=\s])\/.*?\/[gimuy]*(?=[.,;\s])) *| *([+-=\/%(){}\[\]<>|&?!:;,]) *)#s', // Remove unused space characters outside the string and regex
    '#(?|\s*(".*?"|\'.*?\'|(?<=[\(=\s])\/.*?\/[gimuy]*(?=[.,;\s]))\s*|\s*([+-=\/%(){}\[\]<>|&?!:;,])\s*)#s', // Remove unused space characters outside the string and regex
    '#;\}#', // Remove the last semicolon
    '#$#',
    '#;+$#'
  15. @taufik-nurrohman taufik-nurrohman revised this gist Jan 12, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion php-html-css-js-minifier.php
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,7 @@ function minify_js($input) {
    array(
    '#\/\*([\s\S]+?)\*\/|(?<!:)\/\/.*([\n\r]+|$)#', // Remove comments
    '#(^|[\n\r])\s*#', // Remove space and new-line characters at the beginning of line
    '#(?| *(".*?"|\'.*?\'|(?<=[\(=\s])\/.*?\/[igm]*(?=[.,;\s])) *| *([+-=\/%(){}\[\]<>|&?!:;,]) *)#s', // Remove unused space characters outside the string and regex
    '#(?| *(".*?"|\'.*?\'|(?<=[\(=\s])\/.*?\/[gimuy]*(?=[.,;\s])) *| *([+-=\/%(){}\[\]<>|&?!:;,]) *)#s', // Remove unused space characters outside the string and regex
    '#;\}#', // Remove the last semicolon
    '#$#',
    '#;+$#'
  16. @taufik-nurrohman taufik-nurrohman revised this gist Jan 5, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions php-html-css-js-minifier.php
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ function minify_css($input) {
    return preg_replace(
    array(
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|/\*(?>.*?\*/)#s', // Remove comments
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|\s*+;\s*+(})\s*+|\s*+([*$~^|]?+=|[{};,>~+]|\s*!important\b)\s*+|([[(:])\s++|\s++([])])|\s++(:)\s*+(?!(?>[^{}"\']++|"(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')*+{)|^\s++|\s++\z|(\s)\s+#si',
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|\s*+;\s*+(})\s*+|\s*+([*$~^|]?+=|[{};,>~+]|\s*+-(?!\d)|!important\b)\s*+|([[(:])\s++|\s++([])])|\s++(:)\s*+(?!(?>[^{}"\']++|"(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')*+{)|^\s++|\s++\z|(\s)\s+#si',
    '#([\s:])(0)(px|em|%|in|cm|mm|pc|pt|ex)#', // Replace `0(px|em|%|in|cm|mm|pc|pt|ex)` with `0`
    '#:0 0 0 0([;\}])#', // Replace `:0 0 0 0` with `:0`
    '#background-position:0([;\}])#', // Replace `background-position:0` with `background-position:0 0`
    @@ -49,7 +49,7 @@ function minify_js($input) {
    array(
    '#\/\*([\s\S]+?)\*\/|(?<!:)\/\/.*([\n\r]+|$)#', // Remove comments
    '#(^|[\n\r])\s*#', // Remove space and new-line characters at the beginning of line
    '#(?| *(".*?"|\'.*?\'|(?<=[=\(\s])\/.*?\/[igm]*[.,;\s]) *| *([+-=\/%(){}\[\]<>|&?!:;,]) *)#s', // Remove unused space characters outside the string and regex
    '#(?| *(".*?"|\'.*?\'|(?<=[\(=\s])\/.*?\/[igm]*(?=[.,;\s])) *| *([+-=\/%(){}\[\]<>|&?!:;,]) *)#s', // Remove unused space characters outside the string and regex
    '#;\}#', // Remove the last semicolon
    '#$#',
    '#;+$#'
  17. @taufik-nurrohman taufik-nurrohman revised this gist Jan 5, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion php-html-css-js-minifier.php
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ function minify_css($input) {
    return preg_replace(
    array(
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|/\*(?>.*?\*/)#s', // Remove comments
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|\s*+;\s*+(})\s*+|\s*+([*$~^|]?+=|[{};,>~+-]|\s*!important\b)\s*+|([[(:])\s++|\s++([])])|\s++(:)\s*+(?!(?>[^{}"\']++|"(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')*+{)|^\s++|\s++\z|(\s)\s+#si',
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|\s*+;\s*+(})\s*+|\s*+([*$~^|]?+=|[{};,>~+]|\s*!important\b)\s*+|([[(:])\s++|\s++([])])|\s++(:)\s*+(?!(?>[^{}"\']++|"(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')*+{)|^\s++|\s++\z|(\s)\s+#si',
    '#([\s:])(0)(px|em|%|in|cm|mm|pc|pt|ex)#', // Replace `0(px|em|%|in|cm|mm|pc|pt|ex)` with `0`
    '#:0 0 0 0([;\}])#', // Replace `:0 0 0 0` with `:0`
    '#background-position:0([;\}])#', // Replace `background-position:0` with `background-position:0 0`
  18. @taufik-nurrohman taufik-nurrohman revised this gist Jan 3, 2015. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions php-html-css-js-minifier.php
    Original file line number Diff line number Diff line change
    @@ -48,16 +48,18 @@ function minify_js($input) {
    return preg_replace(
    array(
    '#\/\*([\s\S]+?)\*\/|(?<!:)\/\/.*([\n\r]+|$)#', // Remove comments
    '#[\n\r\t]\s*#', // Remove space and new-line characters at the beginning of line
    '#(^|[\n\r])\s*#', // Remove space and new-line characters at the beginning of line
    '#(?| *(".*?"|\'.*?\'|(?<=[=\(\s])\/.*?\/[igm]*[.,;\s]) *| *([+-=\/%(){}\[\]<>|&?!:;,]) *)#s', // Remove unused space characters outside the string and regex
    '#;\}#', // Remove the last semicolon
    '#;*$#'
    '#$#',
    '#;+$#'
    ),
    array(
    "",
    "",
    '$1',
    '}',
    ';',
    ';'
    ),
    $input);
  19. @taufik-nurrohman taufik-nurrohman revised this gist Jan 3, 2015. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions php-html-css-js-minifier.php
    Original file line number Diff line number Diff line change
    @@ -49,14 +49,16 @@ function minify_js($input) {
    array(
    '#\/\*([\s\S]+?)\*\/|(?<!:)\/\/.*([\n\r]+|$)#', // Remove comments
    '#[\n\r\t]\s*#', // Remove space and new-line characters at the beginning of line
    '#(?| *(".*?"|\'.*?\') *| *(\(\/.*?\/[igm]*,) *| *([+-=\/%(){}\[\]<>|&?!:;,]) *)#s', // Remove unused space characters outside the string and regex
    '#;\}#' // Remove the last semicolon
    '#(?| *(".*?"|\'.*?\'|(?<=[=\(\s])\/.*?\/[igm]*[.,;\s]) *| *([+-=\/%(){}\[\]<>|&?!:;,]) *)#s', // Remove unused space characters outside the string and regex
    '#;\}#', // Remove the last semicolon
    '#;*$#'
    ),
    array(
    "",
    "",
    '$1',
    '}'
    '}',
    ';'
    ),
    $input);
    }
  20. @taufik-nurrohman taufik-nurrohman revised this gist Jan 3, 2015. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions php-html-css-js-minifier.php
    Original file line number Diff line number Diff line change
    @@ -5,10 +5,10 @@ function minify_html($input) {
    if(trim($input) === "") return $input;
    return preg_replace(
    array(
    '#\<\!--(?!\[if)([\s\S]+?)--\>#s', // Remove HTML comments except IE comments
    '#\>[^\S ]+#s',
    '#[^\S ]+\<#s',
    '#\>\s{2,}\<#s'
    '#<\!--(?!\[if)([\s\S]+?)-->#s', // Remove HTML comments except IE comments
    '#>[^\S ]+#s',
    '#[^\S ]+<#s',
    '#>\s{2,}<#s'
    ),
    array(
    "",
    @@ -24,8 +24,8 @@ function minify_css($input) {
    if(trim($input) === "") return $input;
    return preg_replace(
    array(
    '#("(?:[^"\\]++|\\.)*+"|\'(?:[^\'\\\\]++|\\.)*+\')|\/\*(?>.*?\*\/)#s', // Remove comments
    '#("(?:[^"\\]++|\\.)*+"|\'(?:[^\'\\\\]++|\\.)*+\')|\s*+;\s*+(})\s*+|\s*+([*$~^|]?+=|[{};,>~+-]|\s*!important\b)\s*+|([[(:])\s++|\s++([])])|\s++(:)\s*+(?!(?>[^{}"\']++|"(?:[^"\\]++|\\.)*+"|\'(?:[^\'\\\\]++|\\.)*+\')*+{)|^\s++|\s++\z|(\s)\s+#si',
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|/\*(?>.*?\*/)#s', // Remove comments
    '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|\s*+;\s*+(})\s*+|\s*+([*$~^|]?+=|[{};,>~+-]|\s*!important\b)\s*+|([[(:])\s++|\s++([])])|\s++(:)\s*+(?!(?>[^{}"\']++|"(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')*+{)|^\s++|\s++\z|(\s)\s+#si',
    '#([\s:])(0)(px|em|%|in|cm|mm|pc|pt|ex)#', // Replace `0(px|em|%|in|cm|mm|pc|pt|ex)` with `0`
    '#:0 0 0 0([;\}])#', // Replace `:0 0 0 0` with `:0`
    '#background-position:0([;\}])#', // Replace `background-position:0` with `background-position:0 0`
  21. @taufik-nurrohman taufik-nurrohman revised this gist Jan 2, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions php-html-css-js-minifier.php
    Original file line number Diff line number Diff line change
    @@ -27,8 +27,8 @@ function minify_css($input) {
    '#("(?:[^"\\]++|\\.)*+"|\'(?:[^\'\\\\]++|\\.)*+\')|\/\*(?>.*?\*\/)#s', // Remove comments
    '#("(?:[^"\\]++|\\.)*+"|\'(?:[^\'\\\\]++|\\.)*+\')|\s*+;\s*+(})\s*+|\s*+([*$~^|]?+=|[{};,>~+-]|\s*!important\b)\s*+|([[(:])\s++|\s++([])])|\s++(:)\s*+(?!(?>[^{}"\']++|"(?:[^"\\]++|\\.)*+"|\'(?:[^\'\\\\]++|\\.)*+\')*+{)|^\s++|\s++\z|(\s)\s+#si',
    '#([\s:])(0)(px|em|%|in|cm|mm|pc|pt|ex)#', // Replace `0(px|em|%|in|cm|mm|pc|pt|ex)` with `0`
    '#:0 0 0 0([;\}])#', // Replace `:0 0 0 0;` with `:0`
    '#background-position:0([;\}])#', // Replace `background-position:0;` with `background-position:0 0;`
    '#:0 0 0 0([;\}])#', // Replace `:0 0 0 0` with `:0`
    '#background-position:0([;\}])#', // Replace `background-position:0` with `background-position:0 0`
    '#(:|\s)0+\\.(\d+)#' // Replace `0.6` to `.6`, but only when preceded by `:` or a white-space
    ),
    array(
  22. @taufik-nurrohman taufik-nurrohman created this gist Jan 2, 2015.
    62 changes: 62 additions & 0 deletions php-html-css-js-minifier.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    <?php

    // HTML Minifier
    function minify_html($input) {
    if(trim($input) === "") return $input;
    return preg_replace(
    array(
    '#\<\!--(?!\[if)([\s\S]+?)--\>#s', // Remove HTML comments except IE comments
    '#\>[^\S ]+#s',
    '#[^\S ]+\<#s',
    '#\>\s{2,}\<#s'
    ),
    array(
    "",
    '>',
    '<',
    '><'
    ),
    $input);
    }

    // CSS Minifier => http://ideone.com/Q5USEF + improvement(s)
    function minify_css($input) {
    if(trim($input) === "") return $input;
    return preg_replace(
    array(
    '#("(?:[^"\\]++|\\.)*+"|\'(?:[^\'\\\\]++|\\.)*+\')|\/\*(?>.*?\*\/)#s', // Remove comments
    '#("(?:[^"\\]++|\\.)*+"|\'(?:[^\'\\\\]++|\\.)*+\')|\s*+;\s*+(})\s*+|\s*+([*$~^|]?+=|[{};,>~+-]|\s*!important\b)\s*+|([[(:])\s++|\s++([])])|\s++(:)\s*+(?!(?>[^{}"\']++|"(?:[^"\\]++|\\.)*+"|\'(?:[^\'\\\\]++|\\.)*+\')*+{)|^\s++|\s++\z|(\s)\s+#si',
    '#([\s:])(0)(px|em|%|in|cm|mm|pc|pt|ex)#', // Replace `0(px|em|%|in|cm|mm|pc|pt|ex)` with `0`
    '#:0 0 0 0([;\}])#', // Replace `:0 0 0 0;` with `:0`
    '#background-position:0([;\}])#', // Replace `background-position:0;` with `background-position:0 0;`
    '#(:|\s)0+\\.(\d+)#' // Replace `0.6` to `.6`, but only when preceded by `:` or a white-space
    ),
    array(
    '$1',
    '$1$2$3$4$5$6$7',
    '$1$2',
    ':0$1',
    'background-position:0 0$1',
    '$1.$2'
    ),
    $input);
    }

    // JavaScript Minifier
    function minify_js($input) {
    if(trim($input) === "") return $input;
    return preg_replace(
    array(
    '#\/\*([\s\S]+?)\*\/|(?<!:)\/\/.*([\n\r]+|$)#', // Remove comments
    '#[\n\r\t]\s*#', // Remove space and new-line characters at the beginning of line
    '#(?| *(".*?"|\'.*?\') *| *(\(\/.*?\/[igm]*,) *| *([+-=\/%(){}\[\]<>|&?!:;,]) *)#s', // Remove unused space characters outside the string and regex
    '#;\}#' // Remove the last semicolon
    ),
    array(
    "",
    "",
    '$1',
    '}'
    ),
    $input);
    }