通过智能自动化 php 脚本结合 CSS 和 JAVASCRIPT
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您如何包含该片段?
据猜测,我想这应该是它自己的文件,并通过
包含> 或类似的。如果您将其直接粘贴到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 theheader()
), hence the plaintext.