Compress javascripts

FIXME This is buggy, don't use it! FIXME

<?php
$content=file_get_contents($_SERVER['QUERY_STRING']);
 
$prot_search=$prot_replace=array();
 
//protect strings and keywords
$content=preg_replace_callback(array('|\\"|','|\\\'|','|\".+\"|','|\'.+\'|'),'cut_unparsed',$content);
 
//strip comments
$content=preg_replace(array('|/\*.*\*/|ms','|//.*$|m'),'',$content);
 
//protect regex
$content=preg_replace_callback('|/.+/|','cut_unparsed',$content);
 
//strip spaces
$content=preg_replace(array('|^\s*|m','|(\w\s+[\$\w\'%])?\s*|','|;(\})|'),'${1}',$content);
 
$content=str_replace($prot_search,$prot_replace,$content);
echo $content;
 
 
function cut_unparsed($matches) {
	global $prot_replace,$prot_search;
	$prot_replace[]=$matches[0];
	return ($prot_search[]='~'.count($prot_replace).'~');
}
 
?>

compatibility

preg_replace_callback (PHP 4 >= 4.0.5, PHP 5)
preg_replace (PHP 4, PHP 5)
str_replace (PHP 4 >= 4.0.5, PHP 5)
file_get_contents (PHP 4 >= 4.3.0, PHP 5)

License

 
wiki/projects/php/js_compress.txt · Last modified: 2009/01/28 15:03 by e-razor
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki