Compress Javascript solved

- Posted in JavaScript by
<?php
header("content-type: text/javascript; charset: UTF-8");
define('_doc_root', dirname(__FILE__,4) . "/");

ob_start("compress");
function minimizeJavascriptSimple($javascript){
    return preg_replace(array("/\s+\n/","/\n\s+/","/ +/"),array("\n","\n "," "),$javascript);
}

function compress($buffer) {

    $buffer = minimizeJavascriptSimple($buffer);
    $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', ' ', $buffer);
    $buffer = preg_replace("/\/\/(.*?)\n\s+/", " ", $buffer);

    $bufferd = explode( "\n", $buffer );
    $buff = [];
    foreach ($bufferd as $buffx) {
        $subBuff = substr($buffx, 0,3);
        if (strpos($subBuff, "//") === false) {
            $buff[] = $buffx;
        }
    }
    $bufferd = implode("", $buff);

    return $bufferd;
}
include(file.js');
ob_end_flush();
Tags: