php str_replace在CSS文件中使用自定义{tags}


php str_replace within a CSS file using custom {tags}

我有一个脚本,根据一些来自html表单的用户输入更新CSS文件。

脚本执行str_replace并在CSS文件中搜索"tags"。如,

html,body {
    background: {bgcolor} url(../images/bg.jpg) repeat-x;
    color: {textcolor};
}

这工作得很好,但显然破坏了CSS文件。最初这并不重要,但由于我的项目中的一个新功能,我需要使用该CSS文件。

所以我想知道是否有更好的替代方案?我在想可能是这样的:

html,body {
    background: #fff /*{textcolor*/ url(../images/bg.jpg) repeat-x;
    color: #fff /*{textcolor*/;
}

所以我可以在标签中使用注释符号,这意味着我的CSS文件没有损坏。唯一的问题是我该如何替换/移除十六进制?

假设您已经准备好了用各自的值替换标记的脚本,为什么不这样做呢:

1:创建一个style.php文件来加载style.css

2: style.php使用您已经创建的函数来替换标签与您的默认值

3: style.php设置报头为header('Content-type: text/css');

4: style.php回显修改过的字符串

然后在整个脚本中调用style.css,而不是调用style.php

preg_replace():

preg_replace("/'3['w'd]{3,6}'s'/'*'{textcolor'}'*'//", $str_hex_code, $str_css_file)