Minify Javascript using php
icon-search
Minify Javascript using php

Minify Javascript using php

Minify Javascript using php


Compressing code on your website decreases or reduces your page size and increases your website SEO.

This is a simple PHP function used to compress or minify JavaScript files.

Using this function you can easily compressed your JavaScript files and get minified or compressed JavaScript code.

PHP Code to Minify Javascript:


I am having two PHP codes for you both can be used to minify or compress JavaScript file.

version 1:
This PHP function deeply compress your JavaScript files.
<?php
function minifyJavascriptCode($javascript){
$blocks = array('for', 'while', 'if', 'else');
$javascript = preg_replace('/([-\+])\s+\+([^\s;]*)/', '$1 (+$2)', $javascript);
// remove new line in statements
$javascript = preg_replace('/\s+\|\|\s+/', ' || ', $javascript);
$javascript = preg_replace('/\s+\&\&\s+/', ' && ', $javascript);
$javascript = preg_replace('/\s*([=+-\/\*:?])\s*/', '$1 ', $javascript);
// handle missing brackets {}
foreach ($blocks as $block){
$javascript = preg_replace('/(\s*\b' . $block . '\b[^{\n]*)\n([^{\n]+)\n/i', '$1{$2}', $javascript);
}
// handle spaces
$javascript = preg_replace(array("/\s*\n\s*/", "/\h+/"), array("\n", " "), $javascript); // \h+ horizontal white space
$javascript = preg_replace(array('/([^a-z0-9\_])\h+/i', '/\h+([^a-z0-9\$\_])/i'), '$1', $javascript);
$javascript = preg_replace('/\n?([[;{(\.+-\/\*:?&|])\n?/', '$1', $javascript);
$javascript = preg_replace('/\n?([})\]])/', '$1', $javascript);
$javascript = str_replace("\nelse", "else", $javascript);
$javascript = preg_replace("/([^}])\n/", "$1;", $javascript);
$javascript = preg_replace("/;?\n/", ";", $javascript);
return $javascript;
}

function getNextKeyElement($javascript){
$elements = array();
$keyMarkers = array('\'', '"', '//', '/*');
foreach ($keyMarkers as $marker){
$elements[$marker] = strpos($javascript, $marker);
}
//regex to detect all regex
$regex = "/[\k(](\/[\k\S]+\/)/";
preg_match($regex, $javascript, $matches, PREG_OFFSET_CAPTURE, 1);
if (count($matches) > 0){
$elements[$matches[1][0]] = $matches[1][1];
}
$elements = array_filter($elements, function($k){return $k !== false;});
if (count($elements) == 0){return false;}
$min = min($elements);
return array($min, array_keys($elements, $min)[0]);
}


function getNonEscapedQuoteIndex($string, $char, $start = 0){
if (preg_match('/(\\\\*)(' . preg_quote($char) . ')/', $string, $match, PREG_OFFSET_CAPTURE, $start)){
if ($match[2][1] == 54){
}
if (!isset($match[1][0]) || strlen($match[1][0]) % 2 == 0){
return $match[2][1];
} else{
return getNonEscapedQuoteIndex($string, $char, $match[2][1] + 1);
}
}
return -1;
}


function minifyJavascripts($filename){
$javascript = file_get_contents($filename);
$buffer = '';
while (list($idx_start, $keyElement) = getNextKeyElement($javascript)){
switch ($keyElement){
case '//':
$idx_start = strpos($javascript, '//');
$idx_end = strpos($javascript, "\n", $idx_start);
if ($idx_end !== false){
$javascript = substr($javascript, 0, $idx_start) . substr($javascript, $idx_end);
} else{
$javascript = substr($javascript, 0, $idx_start);
}
break;
case '/*':
$idx_start = strpos($javascript, '/*');
$idx_end = strpos($javascript, '*/', $idx_start)+2;
$javascript = substr($javascript, 0, $idx_start) . substr($javascript, $idx_end);
break;
default: // must be handle like string case
$idx_start = getNonEscapedQuoteIndex($javascript, $keyElement);
if (strlen($keyElement) == 1){
// quote! Either ' or "
if (substr($javascript, $idx_start, 1) == '\''){
$idx_end = getNonEscapedQuoteIndex($javascript, '\'', $idx_start+1)+1;
} else{
$idx_end = getNonEscapedQuoteIndex($javascript, '"', $idx_start+1) +1;
}
} else{
// regex!
$idx_end = $idx_start + strlen($keyElement);
}
$buffer .= minifyJavascriptCode(substr($javascript, 0, $idx_start));
$quote = substr($javascript, $idx_start, ($idx_end-$idx_start));
$quote = str_replace("\\\n", ' ', $quote);
$buffer .= $quote;
$javascript = substr($javascript, $idx_end);
}
}
$buffer .= minifyJavascriptCode($javascript);
//return $buffer;
$myfile = fopen($filename, "w") or die("Unable to open file!");
fwrite($myfile, $buffer);
fclose($myfile);
}
minifyJavascripts('index.js');
?>

Replace index.js with your javascript file name.

version 2:
This PHP function to compress JavaScript file by simply removing white spaces, blank lines, and tabs etc.

<?php
function minifyJavascripts($filename){
$javascript = file_get_contents($filename);
$fun = preg_replace(array("/\s+\n/", "/\n\s+/", "/ +/"), array("\n", "\n ", " "), $javascript);
$myfile = fopen("new.js", "w") or die("Unable to open file!");
fwrite($myfile, $fun);
fclose($myfile);}
minifyJavascripts('index.js');
?>

Replace index.js with your javascript filename.

I recommend you to use version 1 because it deeply compress your JavaScript code.
Disqus Codes

No comments

Notifications

Disqus Logo