random1 Posted September 4, 2009 Share Posted September 4, 2009 Hey All , 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? Link to comment https://forums.phpfreaks.com/topic/173068-creating-a-php-function-to-optimize-javascript-file/ Share on other sites More sharing options...
RichardRotterdam Posted September 4, 2009 Share Posted September 4, 2009 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. Link to comment https://forums.phpfreaks.com/topic/173068-creating-a-php-function-to-optimize-javascript-file/#findComment-912273 Share on other sites More sharing options...
bundyxc Posted September 4, 2009 Share Posted September 4, 2009 Regardless, our regex buddies might be of more help with this. Link to comment https://forums.phpfreaks.com/topic/173068-creating-a-php-function-to-optimize-javascript-file/#findComment-912286 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.