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 3214430
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:03:36+00:00 2026-05-17T15:03:36+00:00

I have found utilities that can compress html, javascript, and css files individually. However,

  • 0

I have found utilities that can compress html, javascript, and css files individually. However, lets say I have a file called homepage.php with php at the top of the page, css and javascript in the header, html in the body along with more php code and perhaps some more javascript. Sometimes, the chunks with javascript may also have php in it (for transferring php values to javascript). Are there any utilities that can handle compressing such pages?

Going through every file, breaking it apart manually, and compressing everything individually can get very tedious. Even something that can just compress the html and css in the files and ignore the javascript and php would be extremely useful. Most of my javascript is in external js files anyway.

  • 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-17T15:03:37+00:00Added an answer on May 17, 2026 at 3:03 pm

    Thanks for all the feedback guys. I’ll be using apache to compress the files but wanted to make something to remove all the line breaks in css and html segments of the page before hand. I couldn’t find anything to do that while leaving the php and javascript untouched in a file, so I made my own script to do it.

    The following code can definitely be improved and is very, very raw. There’s a lot of spots where I can make it more efficient, but this is just a test of an idea. However, it’s working well enough to use. Just save it to a php file, and set $file_name to the name of your file.

    <?
    
    function minifyFile($file)
    {
    
        $contents = file_get_contents($file);
    
        $contents = preg_replace('/<!--(.|\s)*?-->/', '', $contents);
        $contents = str_replace('<?', ' <? ', $contents);
        $contents = str_replace('?>', ' ?> ', $contents);
        $contents = str_replace('<script', ' <script', $contents);
        $contents = str_replace('script>', 'script> ', $contents);
    
        $filtered = '';
    
        $length = strlen($contents);
    
        $ignore = Array();
        $html = Array();
    
        for($i = 0;$i <= $length;$i++)
        {
    
            if(substr($contents, $i, 2) == '<?')
            {
    
                $end = strpos($contents, '?>', $i) + 2;
    
                array_push($ignore, Array('php', $i, $end));
                $i = $end;
    
            }
            else if(strtolower(substr($contents, $i, 7)) == '<script')
            {
    
                $end = strpos($contents, '</script>', $i) + 9;
    
                array_push($ignore, Array('js', $i, $end));
                $i = $end;
    
            }
    
        }
    
        $ignore_c = count($ignore) - 1;
    
        for($i = 0;$i <= $ignore_c;$i++)
        {
    
            $start = $ignore[$i][2];
    
            if($start < $length)
            {
    
                array_push($html, Array('html', $start+1, $ignore[$i+1][1]-1));
    
            }
    
        }
    
        function cmp($a, $b)
        {
        if ($a[1] == $b[1]) {
            return 0;
        }
        return ($a[1] < $b[1]) ? -1 : 1;
        }
    
        $parts = array_merge($ignore, $html);
    
        usort($parts, "cmp");
    
        foreach($parts as $k => $v)
        {
    
            $cont = substr($contents, $parts[$k][1], ($parts[$k][2]-$parts[$k][1]));
    
            if($parts[$k][0] == 'html')
            {
    
                $cont = str_replace(Array("\n", "\t", "  ", "   ", "    "), " ", $cont);
    
            }
    
            $filtered .= $cont;
    
        }
    
        return $filtered;
    
    }
    
    $file_name = '../main.php';
    $filtered = minifyFile($file_name);
    
    echo '<textarea style="width:700px;height:600px">' . file_get_contents($file_name) . '</textarea>';
    echo '&nbsp;&nbsp;<textarea style="width:700px;height:600px">' . $filtered . '</textarea>';
    
    ?>
    

    With some tinkering, this code can be modified to iterate through all the files in a directory and save them to another directory as minified versions.

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

Sidebar

Related Questions

I have found some similar questions like this one, however there are so many
I have found in sfDoctrineApplyPlugin a template called applyAfter.php that shows a message like
I have a Java application, built with eclipse, that uses QTJambi. Running the application
I have found these few lines of assembly in ollydbg: MOV ECX,DWORD PTR DS:[xxxxxxxx]
I have found a fairly insignificant bug in SQL Server 2008. I would like
I know this a is a common issue, but everything I can find in
I have a bunch of PDF documents and all of them contain a title
I've made a c# class that I'm trying to run in PowerShell with add-Type.
I am creating a site that utilizes the jQuery UI tabs . Whenever the
I've this question from an assignment to create a Store which rent out books,

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.