0

I am looking for a way to combine external css files , and even inline css codes with php , before load html , and then add them into a external file with all css codes in page .

I want to add this ability for js files too .

currently I have a minify html function which delete spaces and new lines from html .

function _minify_html($input) {
    return preg_replace_callback('#<\s*([^\/\s]+)\s*(?:>|(\s[^<>]+?)\s*>)#', function($m) {
        if(isset($m[2])) {

            return '<' . $m[1] . preg_replace(
                array(
                    // From `defer="defer"`, `defer='defer'`, `defer="true"`, `defer='true'`, `defer=""` and `defer=''` to `defer` [^1]
                    '#\s(checked|selected|async|autofocus|autoplay|controls|defer|disabled|hidden|ismap|loop|multiple|open|readonly|required|scoped)(?:=([\'"]?)(?:true|\1)?\2)#i',
                    // Remove extra white-space(s) between HTML attribute(s) [^2]
                    '#\s*([^\s=]+?)(=(?:\S+|([\'"]?).*?\3)|$)#',
                    // From `<img />` to `<img/>` [^3]
                    '#\s+\/$#'
                ),
                array(
                    // [^1]
                    ' $1',
                    // [^2]
                    ' $1$2',
                    // [^3]
                    '/'
                ),
            str_replace("\n", ' ', $m[2])) . '>';
        }
        return '<' . $m[1] . '>';
    }, $input);
}


function minifyHtml($buffer) {
    $buffer = _minify_html($buffer);
    return 
        // remove ws outside of all elements
        preg_replace( '/>(?:\s\s*)?([^<]+)(?:\s\s*)?</s', '>$1<', 
            // remove ws around all elems excepting script|style|pre|textarea elems
            preg_replace(
            '/\s+(<\\/?(?!script|style|pre|textarea)\b[^>]*>)/i', '$1',
                // trim line start
                preg_replace( '/^\s\s*/m', '', 
                    // trim line end
                    preg_replace( '/\s\s*$/m', '', 
                        // remove HTML comments (not containing IE conditional comments)
                        preg_replace_callback( 
                            '/<!--[^ShowThitComment]([\s\S]*?)-->/', 
                            function( $m ) {
                                return ( 0 === strpos($m[1], '[' ) || false !== strpos( $m[1], '<![' ) ) ? $m[0] : '';
                            },
                            // start point
                            $buffer 
                        )
                    )
                )
            )
        )
    ;
}

thanks in advance

1 Answer 1

2

You would achieve what you want more efficiently using Gulp. Look at gulp-clean-css for minifying css and gulp-uglify for minifying js.

To get started with gulp here is a good place.

Sign up to request clarification or add additional context in comments.

1 Comment

You can also check also: gruntjs.com It works with watch, less and uglify. You can configure the enviroment to compile everything when you save a file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.