如何动态更改css文件中的图像文件夹名称?

时间:2010-12-25 07:04:01

标签: php css

我的项目是PHP,我需要动态更改css文件中的图像文件夹名称,

即,

# all.css file

.html{
background: url(../img/login.gif) no-repeat scroll 0 -36px;
.....
}

我需要将文件夹名称“img”更改为“image”或“styleimg”等,具体取决于templatewise。如何使用php文件?

2 个答案:

答案 0 :(得分:2)

使用PHP来提供CSS。使用header()将内容类型设置为text / css,输出标签之外的大部分CSS(但在设置标题之后)。在这个PHP中找出所选模板,将所需的目录名称分配给变量,比如$ imgloc。

然后,在您需要输出图像名称的行上,执行以下操作:

background: url(../<?= $imgloc ?>/login.gif) no-repeat scroll 0 -36px;

答案 1 :(得分:0)

<?php
$file = xyz.css;
$handle = @fopen($file, "r"); // Open file form read.

$line_match = "background: url(../img/login.gif) no-repeat scroll 0 -36px;\n";

$toThis = "background: url(../img/new_login_image.gif) no-repeat scroll 0 -36px;\n";

if ($handle) {
while (!feof($handle)) // Loop til end of file.
{
$buffer .= fgets($handle, 4096); // Read a line.
if ($buffer == $line_match) // Check for string.
{
echo "$buffer"; // If not string show contents.
} else {
$buffer .= $toThis; // If string, change it.
echo "$buffer"; // and show changed contents.
}
}

$Handle = fopen($file, 'w');
$Data = $buffer; 
fwrite($Handle, $Data); 


fclose($handle); // Close the file.
}

?>

也许从vars $ linematch和$ to下降\ n即时通讯这不确定! - 试试两个。