如何在 html 中压缩动态代码

How to compress dynamic code in html?

本文关键字:压缩 动态 代码 html      更新时间:2023-09-26

大家好,任何人都可以帮我压缩html5,css3和php中的动态代码吗?

Is对于PHP"代码"毫无用处,因为它在服务器端运行。

对于html,css,javascript,您可以安装gulp.js以及一些用于任务的专用插件,例如用于缩小css的gulp-minify-css和用于缩小html的gulp-minify-html。

如果你做了任何努力,比如做谷歌搜索,你应该找到这个:

<?php
    /*  start the output buffer  */
    ob_start('compress_page');
    /*  xhtml code below  */
?>
<!-- all xhtml content here -->
<?php
    /*  end the buffer, echo the page content  */
    ob_end_flush();
    /*  function that gets rid of tabs, line breaks, and extra spaces  */
    function compress_page($buffer) {
        $search = array('/>[^S ]+/s', '/[^S ]+</s', '/(s)+/s');
        $replace = array('>', '<', '1');
        return preg_replace($search, $replace, $buffer);
    }
?>

正如其他人指出的那样,您可以使用任务运行程序来缩小您的 CSS 和 JS。

同时,您的问题被错误地标记,因为它与JavaScript无关。

   <?php
        /*  start the output buffer  */
        ob_start('compress_page');
        /*  xhtml code below  */
          /*  end the buffer, echo the page content  */
        ob_end_flush('compress_page');
        /*  function that gets rid of tabs, line breaks, and extra spaces  */
        function compress_page($buffer) {
                // remove comments, tabs, spaces, newlines, etc.
                $search = array(
                    "/'/'*(.*?)'*'/|['t'r'n]/s" => "",
                    "/ +'{ +|'{ +| +'{/" => "{",
                    "/ +'} +|'} +| +'}/" => "}",
                    "/ +: +|: +| +:/" => ":",
                    "/ +; +|; +| +;/" => ";",
                    "/ +, +|, +| +,/" => ","
                );
                $buffer = preg_replace(array_keys($search), array_values($search), $buffer);
                return $buffer;
        }
    ?>

终于工作^_^