我认为您正在寻找“file_put_contents”,
请参阅:http : //php.net/manual/en/function.file-put-contents.php
我假设您想获取用户使用某种形式输入的值?从作为 PHP 字符串创建您的 css 声明
然后使用 file_put_contents 函数将字符串写入 css 文件。
$cssString = '.selector { background-color: '.$userSubmittedBackgroundColor.'; }';
file_put_contents('populated.css', $cssString);
Run Code Online (Sandbox Code Playgroud)
编辑:
或者,您创建基本 .css 文件并使用 string_replace 或类似的东西交换参数。我想你会想一次做多个替换,所以看看'strtr'函数,它基本上是一个字符串替换,但接受一个键值对数组作为替换标记和值。
基础文件
.selector { background-color: [*backgroundColorPlaceholder*]; }
Run Code Online (Sandbox Code Playgroud)
PHP
$cssString = file_get_contents('base.css');
$cssString = str_replace('[*backgroundColorPlaceholder*], $userSubmittedBackgroundColor, $cssString);
file_put_contents('populated.css', $cssString);
Run Code Online (Sandbox Code Playgroud)