PHP Code Compressor

PHP Compressor removes all the comments and empty spaces from PHP files making them smaller in size. Comparing to an obfuscator, PHP Code Compressor does not change variable/function names - creates no naming conflicts.

The script recursively opens the input directory, strips unneeded code from PHP files and copies all non-php files to the output directory.

<?php
/**
 * PHP Code compressor
 * 
 * @author Piotr Polak
 * @version 0.1
 * @license see http://www.polak.ro/licencing-projects.html
 * @link http://www.polak.ro/php-code-compressor.html
 */


function strip_php_comments(&$str)
{
	$str = str_replace("<?php", '<?php ', $str);
	$str = str_replace("\r", '', $str);
	$str = ereg_replace("/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/", '', $str);
	$str = ereg_replace("//[\x20-\x7E]*\n", '', $str);
	$str = ereg_replace("#[\x20-\x7E]*\n", '', $str);
	$str = ereg_replace("\t|\n", '', $str);
}


function cc_files($path = '', $destination = './output/')
{
	if($path == '' || $path == './') return;
	
	
	@mkdir($destination);
	
	$dir = opendir($path);
	while($file = readdir($dir))
	{
		if($file == '' || $file == '.' || $file == '..') continue;
		
		if(is_dir($path.'/'.$file))
		{
			cc_files($path.'/'.$file, $destination.'/'.$file.'/');
		}
		else
		{
			// for files
			$contents = file_get_contents($path.'/'.$file);
			
			if(strtolower(substr($file, strrpos($file, '.')+1)) == 'php')
			{
				strip_php_comments($contents);
			}
			
			file_put_contents($destination.'/'.$file, $contents);
			
		}
		
	}
}

cc_files('./input/', './output');
?>
Powered by PepisCMS