x

Active Group: Uniquez TalkBox

  • General
  • Whispers
  • Groups

Please log-in OR Register to view the Talkbox
Everyone must make a new account!


PHP : Minify Javascript

  • 1 Replies
  • 428 Views

0 Members and 1 Guest are viewing this topic.

*

Alcaline

  • *
  • Newbie
  • 20 7
  • Oo Laaa Laaa
PHP : Minify Javascript
« : February 04, 17, 01:04 PM »
My php helper function to minify JS :

Code: [Select]
//=======================================================
function minifyJS($buffer) {
    $buffer = preg_replace("/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/", "", $buffer);
    $buffer = str_replace(["\r\n","\r","\t","\n",'  *',"//*\r\n","//*\r"], "", $buffer);
    $buffer = preg_replace([
      '/\s*(?:(?=[=\-\+\|%&\*\(\)\[\]\{\};:\,\.\<\>\!\@\#\^`~]))/',
      '/(?:(?<=[=\-\+\|%&\*\(\)\[\]\{\};:\,\.\<\>\?\!\@\#\^`~]))\s*/',
    ], '', $buffer);
    $buffer = preg_replace(
        ['#\btrue\b#', '#\bfalse\b#', '#\breturn\s+#'],
        ['!0', '!1', 'return '],
    $buffer);

    return preg_replace('!\s+!', ' ', $buffer);
}

This code delete :
  • Comments
  • spaces
  • newline
And it replace booleans by their values

*

oOo--STAR--oOo

  • *
  • Full Member
  • Founder
  • 140 33
  • Nostradamus feeling super marvellous.
Re: PHP : Minify Javascript
« Reply #1: February 10, 17, 05:41 PM »
Awesome lightweight script.

I do use these services for minifying CSS and JS which you can find on Googles PageSpeed Insights. I basically go straight to the websites and paste my CSS/JS and paste them in my Minified scripts, while also combining what I can.

https://css.github.io/csso/csso.html
http://lisperator.net/uglifyjs/

I really like the uglify JS one and only use the demo really :D

Thanks for sharing!
You can't fool a sufficiently talented fool!