【问题标题】:How to compress dynamic code in html? [closed]如何压缩html中的动态代码? [关闭]
【发布时间】:2014-12-11 08:46:30
【问题描述】:

大家好,谁能帮我压缩html5、css3和php中的动态代码?

【问题讨论】:

  • compress 是什么意思?
  • 动态代码是什么意思?
  • 代码是什么意思?
  • 帮助是什么意思?
  • 我的意思是当你在一个页面中查看页面源代码时没有空白

标签: javascript php html


【解决方案1】:

对于 PHP“代码”来说是无用的,因为它在服务器端运行。

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

【讨论】:

    【解决方案2】:

    如果您像 Google 搜索一样努力,您应该会找到 this

    <?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 没有关系。

    【讨论】:

    • 非常感谢。仅在 HTML 和 JS 上工作
    • 如果它对你有用或引导你找到解决方案,那么你应该接受这个作为答案。
    【解决方案3】:
       <?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;
            }
        ?>
    

    终于成功了^_^

    【讨论】:

    • 伙计,你的顾虑很少:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 2019-12-25
    • 2010-11-12
    相关资源
    最近更新 更多