Compress web pages with php, javascript, html and css in them

I found utilities that can compress html, javascript and css files separately. However, let's 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 a lot of php code, and possibly some more javascript. Sometimes javascript pieces can also contain php (to pass php values ​​to javascript). Are there any utilities that can handle the compression of such pages?

Going through each file, breaking it manually, and compressing everything individually can become very tedious. Even that which can simply compress html and css in files and ignore javascript and php would be extremely useful. Most of my javascript in all external js files.

+3
source share
4 answers

Thanks for the feedback. I will use apache to compress the files, but would like to do something to remove all line breaks in the CSS and html segments of the page. I could not find anything that could do this, leaving php and javascript intact in the file, so I made my own script to do this.

The following code can certainly be improved and very, very crude. There are many places where I can make it more effective, but this is just a test of the idea. However, it works well enough to use. Just save it in php file and give $ file_name 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>';

?>

.

0

, apache ( , apache - ). PHP HTML- . mod_deflate, .

+2

, - , DOM. <script> <style>. , JsMin CSSMin, .

gzip -, -. ?

+1

html, js css , , .

? php ?

, , gzip - . .

0

Source: https://habr.com/ru/post/1767187/


All Articles