压缩含有 php、javascript、html 和 css 的网页

发布于 2024-09-25 09:31:50 字数 358 浏览 3 评论 0原文

我找到了可以单独压缩 html、javascript 和 css 文件的实用程序。但是,假设我有一个名为 homepage.php 的文件,其中 php 位于页面顶部,css 和 javascript 位于页眉中,html 位于正文中,还有更多 php 代码,也许还有更多 javascript。有时,带有 javascript 的块中也可能包含 php(用于将 php 值传输到 javascript)。是否有任何实用程序可以处理压缩此类页面?

检查每个文件、手动将其分解并单独压缩所有内容可能会变得非常乏味。即使只是压缩文件中的 html 和 css 而忽略 javascript 和 php 的东西也会非常有用。无论如何,我的大部分 javascript 都在外部 js 文件中。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

榆西 2024-10-02 09:31:50

如果这个问题背后的想法是使用更少的带宽,那么我建议您使用 apache 输出过滤器(假设您无论如何都使用 apache 作为网络服务器)。只需使用 ie 过滤所有 PHP 脚本和静态 HTML 页面即可。 mod_deflate,您将使用更少的带宽。

If the idea behind this question is to use less bandwidth, then I suggest you use an apache output filter (assuming that you are using apache as webserver anyway). Just filter all PHP scripts and static HTML pages with ie. mod_deflate, and you'll use less bandwidth.

瀟灑尐姊 2024-10-02 09:31:50

即使没有工具可以让您一次完成所有小型化,您也可以使用 DOM 轻松实现自动化。只需解析页面并找到所有

但是,您也可以在网络服务器中启用 gzip 压缩,这对于大多数网站来说应该是足够的压缩。或者您最近遇到过流量限制吗?

Even if there is not a tool that would let you do all minifaction at once, you could automate this easily with DOM. Just parse the page and find all <script> and <style> elements. Then run their content through third party libs like JsMin or CSSMin and replace the nodes with the output.

However, you could also just enable gzip compression in your webserver, which should be more than enough compression for most websites. Or did you run into any traffic limits lately?

浪推晚风 2024-10-02 09:31:50

通常,对 html、js 和 css 文件进行压缩是为了减少文件发送到用户浏览器时的带宽使用,从而加快页面加载速度。

这是你的目的吗?为什么要将 php 发送到浏览器?

如果您的目的不是更快的页面加载而是节省磁盘空间,您可以使用 gzip 或其他东西。但这似乎几乎毫无意义。

Normally, compression of html, js and css files is done to reduce bandwidth usage as the files are sent to the user's browser, resulting in faster page loads.

Is this your purpose? Why would you want to send php to the browser?

If your purpose is not faster page loads but saving disk space, you could just use gzip or something. But it seems almost pointless.

云朵有点甜 2024-10-02 09:31:50

感谢大家的反馈。我将使用 apache 来压缩文件,但想要预先删除页面的 css 和 html 段中的所有换行符。在将 php 和 javascript 保留在文件中不变的情况下,我找不到任何可以执行此操作的内容,因此我编写了自己的脚本来执行此操作。

下面的代码绝对可以改进,而且非常非常原始。有很多地方我可以提高效率,但这只是对一个想法的测试。不过,它的性能足够好,可以使用。只需将其保存到 php 文件,并将 $file_name 设置为文件名即可。

<?

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 '  <textarea style="width:700px;height:600px">' . $filtered . '</textarea>';

?>

通过一些修补,可以修改此代码以迭代目录中的所有文件并将它们作为缩小版本保存到另一个目录。

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 '  <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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文