Jump to content

Creating a PHP function to optimize Javascript file?


random1

Recommended Posts

Hey All :D,

 

I already have 2 functions that optimize HTML, CSS but I need one that optimizes/minifies JavaScript files.

 

public function optimizeJavascript($buffer)
{
	/* remove comments */
	$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
	/* remove tabs, spaces, new lines, etc. */
	$buffer = str_replace(array("\r\n", "\r", "\n", "\t",
	'  ', '    ', '    '), '', $buffer);
	/* remove unnecessary spaces */

	return $buffer;
}

 

Any suggestions on what parts of Javascript files can be filtered out of an optimized file?

Sounds a bit like you're trying to re-invent the weel. Are you doing this for fun/practive or do you want the best optimized javascript source. If it is the last option you might want to look up "yui compressor" or jsmin.

 

If it is for practice maybe you can write a function that shortens certain variable names.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.