【问题标题】:Change the directory of all images in a CSS file更改 CSS 文件中所有图像的目录
【发布时间】:2011-02-03 14:06:16
【问题描述】:

这个问题与我发布的一个 earlier: 有关,我这里有包含此代码的 css 文件:

body {
    background-image:url(images/background.gif);
    color: #7a7e33;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 12px;
    margin: 0px;
    padding: 0px;
}

#header {
    background-image:url(images/header.jpg);
    width: 707px;
    height:300px;
    display:block;
    margin-left: 240px;
    padding:0px;
}
#content{
    background-image:url(images/sheetmiddle.gif);
    background-repeat:repeat-y;
    position:relative;
}
#sheettop{
    background-image:url(images/sheet.gif);
    background-repeat:no-repeat;
    width:707px;
}
#footer{
    background-image:url(images/footer.gif);
    width:707px;
    height:86px;
    margin-left:242px;
}

如您所见,有许多图像目录。我想添加../,这样所有的图像目录都将如下所示:

background-image:url(../images/sheet.gif);

以及所有其他图像目录。 如何制作一个自动读取 style.css 然后插入的 php 代码:../

来自我previous post的回答,

$css = str_replace("url(images/", "url(../images/", $css);

我在这里还缺少什么?

这是我的学校项目。请帮忙。谢谢!

【问题讨论】:

  • 为什么不直接打开 style.css 并进行搜索和替换?
  • 你需要知道如何打开 CSS 文件并阅读它吗?
  • 你从$css 得到什么 - 什么不起作用。 str_replace 看起来不错。
  • 如果现在解析器项目就是这样,叹息。
  • 我认为这是一个更大问题的一部分,即根据主题更改图像,请询问我们,因为这可能不是最好的解决方案。

标签: php css parsing


【解决方案1】:
$css = file_get_contents('/path/to/your/css/file'); // slurp in the file
$css = str_replace(...);  // modify the data
file_put_contents('/path/to/your/css/file', $css); // spit it back out

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    • 2012-08-14
    • 1970-01-01
    • 2012-02-12
    相关资源
    最近更新 更多