Jump to content

Creating a PHP function to optimize Javascript file?


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.