CSS与PHP合并 [英] CSS merging with PHP
本文介绍了CSS与PHP合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在网站上找到了这个网址,
I found this on a website,
css_loader.php
css_loader.php
<?php
// First of all send css header
header("Content-type: text/css");
// Array of css files
$css = array(
'main.css',
'menu.css',
'content.css'
);
// Loop the css Array
foreach ($css as $css_file) {
// Load the content of the css file
$css_content = file_get_contents($css_file);
// print the css content
echo $css_content;
}
?>
向页面添加CSS
<link href="css_loader.php" rel="stylesheet" type="text/css" />
看起来很合乎逻辑,但是当我将它应用到我的页面时,它没有工作。
It looks very logical but when i applied it to my page, it didnt work.
是否可以以这种方式合并CSS文件?
is that possible to merge CSS files with this way ?
推荐答案
您的代码中有错误,请输入正确的代码:
You have an mistake in your code, here's the correct code:
<?php
// First of all send css header
header("Content-type: text/css");
// Array of css files
$css = array(
'main.css',
'menu.css',
'content.css'
);
// Prevent a notice
$css_content = '';
// Loop the css Array
foreach ($css as $css_file) {
// Load the content of the css file
$css_content .= file_get_contents($css_file);
}
// print the css content
echo $css_content;
?>
我希望文件在同一个文件夹中。也许你应该使用 __ DIR __
或 dirname(__ FILE __)
来获取文件的相对路径。
And I hope the files are in the same folder. Perhaps you should use __DIR__
or dirname(__FILE__)
to get the relative path to your files.
这篇关于CSS与PHP合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文