<?php
if (!function_exists('advanced_minify')) {

    function advanced_minify($buffer) {

        // Remove HTML comments
        $buffer = preg_replace('/<!--(?!\[if).*?-->/s', '', $buffer);

        // Remove whitespace between tags
        $buffer = preg_replace('/>\s+</', '><', $buffer);

        // Minify CSS only
        $buffer = preg_replace_callback('/<style\b[^>]*>(.*?)<\/style>/is', function ($matches) {
            $css = $matches[1];
            $css = preg_replace('/\/\*.*?\*\//s', '', $css);
            $css = preg_replace('/\s+/', ' ', $css);
            $css = str_replace([' {', '{ '], '{', $css);
            $css = str_replace([' }', '} '], '}', $css);
            $css = str_replace('; ', ';', $css);
            $css = str_replace(': ', ':', $css);
            return '<style>' . trim($css) . '</style>';
        }, $buffer);

        return trim($buffer);
    }

}

ob_start("advanced_minify");
?>