Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7435953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:09:42+00:00 2026-05-29T10:09:42+00:00

I have some JavaScript, which is generated by PHP code, using a template. The

  • 0

I have some JavaScript, which is generated by PHP code, using a template.

The result of the template is a file contain JavaScript but with statements scattered through out it.

I want to reduce the size of this file by removing white space and comments.

However all I can find on the web are tools for plain JavaScript, which are extremely complex.

I have all ready write a script to reduce HTML, and CSS based content using the same system, I’m looking for info on reducing the size of JavaScript file so I can write a reduction script that can handle the PHP code being in the file.

So what I want is:

A tutorial for reducing the size of JavaScript code.
A simple to follow script for reducing JavaScript size I can analysis.
Or a Tool that works fine with PHP inline with the JavaScript Code (Must be either Public Domain, or Open Source allowing for use in commercial works)

EDIT:

$script = preg_replace('%/\*.*?\*/%','',$script);
$compress = preg_split('%("|\'|/)(?:\\\\|\\\1|[^\1])*?\1|<\?php\s+(?:("|\')(?:\\\\|\\\2|[^\2])*?\2|[^"\']*?)*?\?>%',$script);
preg_match_all('%(("|\'|/)(?:\\\\|\\\2|[^\2])*?\2|<\?php\s+(?:("|\')(?:\\\\|\\\3|[^\3])*?\3|[^"\']*?)*?\?>)%',$script,$no_compress);
foreach ($compress as &$block) { $block = preg_replace('%\s+%',' ',$block); }

I think I’ve solved it, the above should remove C-style comments, then split the remaining code around PHP blocks, Javascript Strings ‘ or “, and Regex Patterns /, and also extract the code blocks, strings, and Regex Patterns, Code inside a Pattern or String should be ignored as its second the Option in Regex.
The Final line should reduce the white space in the compress blocks to a single space

Then It should just be a matter of recombination of the blocks, i.e.

$cscript .= $compress[$i].$no_compress[$i]

To rebuild the script.

Any thing I’ve missed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:09:43+00:00Added an answer on May 29, 2026 at 10:09 am

    Do you want something like this?

    http://code.google.com/p/minify/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using some pre-written JavaScript from polldaddy. They have a JavaScript option which,
i have some javascript roll over code that works fine in firefox but when
I totally want to avoid using inline javascript! But i have a table which
I have a PHP-generated page which is displaying some data about a set of
I have a web page with some auto-generated javascript content which manipulates the DOM
I have some javascript which catches changes to a form then calls the form's
Hi I have a usercontrol which includes some JavaScript, if I add the control
I have some javascript that I'm trying to retool using jQuery to learn the
I have some Javascript code that will programmatically register an COM interop assembly by
I have some JavaScript code that works in IE containing the following: myElement.innerText =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.