通过智能自动化 php 脚本结合 CSS 和 JAVASCRIPT

发布于 2024-10-06 22:25:50 字数 1577 浏览 6 评论 0原文

this 讨论中找到了一个不错的程序它将 CSS 文件合并为一个,以加快和加快 CSS 加载。它可以工作并将我所有的 css 文件放入一个小堆中,但是页面显示为文本(apache 认为网页变成了 css 文件)可能出了什么问题?

我做了什么:我保存了以下内容脚本作为 php 文件并将其包含到我的页面中,并将 url 更改为 css 文件。 CSS 加载正常,但网页在浏览器中显示为纯文本!虽然所有 css 都在那里,其余的 php 生成的项目也在那里,唯一的事情是它应该加载的浏览器内容是纯文本,而不是 html 网站...有任何线索吗?

<?php
  header('Content-type: text/css');
  ob_start("compress");
  function compress($buffer) {
    /* remove comments */
    $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
    /* remove tabs, spaces, newlines, etc. */
    $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
    return $buffer;
  }

  /* your css files */
  include('master.css');
  include('typography.css');
  include('grid.css');
  include('print.css');
  include('handheld.css');

  ob_end_flush();
?>

此外,一旦起作用,以后可以进行改进,甚至提高其强度:

"; }" > "}"(2 个字符)
“{”> “{”(1 个字符)
" {" > "{" (1char)
" :" > > ":"(1 个字符)
": " > > ":"(1 个字符) “,”> ","(1 个字符)
", " > ","(1 个字符) " (" > "(" (1 个字符)
"( " > "(" (1 个字符) " )" > > ")"(1 个字符)
") " > ")"(1 个字符)

found a nice program in this discusson that combines CSS files into one, to speed up and fasten the CSS load. It works and makes all my css files into one tiny heap, BUT the page shows as text (the webpage becomes a css file, apache thinks) what can be wrong?

What i did: I saves the below script as a php file and included that into my page, and changed the urls to the css files with theirs. The css loads fine, but the WEBPAGE shows up as plain text into the browser! while all css is there and the rest of the php generated items too, only thing is the browser things it should load is as plain text, instead of html website... Any clues?

<?php
  header('Content-type: text/css');
  ob_start("compress");
  function compress($buffer) {
    /* remove comments */
    $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
    /* remove tabs, spaces, newlines, etc. */
    $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
    return $buffer;
  }

  /* your css files */
  include('master.css');
  include('typography.css');
  include('grid.css');
  include('print.css');
  include('handheld.css');

  ob_end_flush();
?>

Furthermore improvements could be made later to even improve its strength, once working:

"; }" > "}" (2 chars)
"{ " > "{" (1 char)
" {" > "{" (1char)
" :" > ":" (1 char)
": " > ":" (1 char)
" ," > "," (1 char)
", " > "," (1 char)
" (" > "(" (1 char)
"( " > "(" (1 char)
" )" > ")" (1 char)
") " > ")" (1 char)

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

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

发布评论

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

评论(1

最佳男配角 2024-10-13 22:25:50

您如何包含该片段?

据猜测,我想这应该是它自己的文件,并通过 包含> 或类似的。如果您将其直接粘贴到index.php中,则将其输出的MIME类型设置为text/css(通过header()),因此是纯文本。

How are you including that snippet?

At a guess, I'd imagine that should be it's own file and included via <link rel="stylesheet" type="text/css" href="./css.php" /> or similar. If you pasted that straight into index.php, you're setting the MIME type of it's output to text/css (via the header()), hence the plaintext.

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