PHP 如何覆盖文件的特定部分
PHP How to overwrite a specific section of a file
我有一个小 CSS 文件。
:root {
--header-bg:
#004d73;
--header-title-color:
#dbebfa;
}
我正在尝试编写一个接受输入值并用新数据覆盖CSS 文件中的特定行 的脚本。这是我目前所拥有的。
$path = 'custom-test.css';
if (isset($_POST['header-bg'])) {
$fh = fopen($path,"a+");
$string = $_POST['header-bg'];
fwrite($fh,$string,7); // Write information to the file
fclose($fh); // Close the file
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
这是可行的,当然它只是将数据附加到文件末尾。
我不知道是否需要通过 fopen、fwrite 指定要覆盖的行,或者我是否需要脚本将整个文件打开到数组中(即 way 超出了我的基本 PHP 技能)。
同样,我的技能很有限。欢迎所有提示,但扩展我现有代码的建议将非常有帮助。
顺便说一句,我不能为此使用数据库条目。
最好的标准方法是制作一个模板文件,然后使用参数渲染该文件。
看看this
首先制作一个这样的模板文件:
:root {
--header-bg: <?=$this->e($headerBg)?>;
--header-title-color: <?=$this->e($headerTitleColor)?>;
}
并在您的 php 脚本中:
$path = './';
$template = 'custom-test.css';
$output = 'output-test.css';
if (isset($_POST['header-bg'])) {
$params = [
'headerBg' => $_POST['header-bg'],
'headerTitleColor' => ''
];
$templates = new League\Plates\Engine($path);
file_put_content($output, $templates->render($template, $params));
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
您必须使用 composer 安装此软件包(如果您使用的是 composer):
composer require league/plates
然后在你的脚本中导入它require 'vendor/autoload.php'
如果您不使用作曲家,则必须 download it 并在脚本中包含以下文件
"src/Template/match.php",
"src/Extension/Data/data.php",
"src/Extension/Path/path.php",
"src/Extension/RenderContext/func.php",
"src/Extension/RenderContext/render-context.php",
"src/Extension/LayoutSections/layout-sections.php",
"src/Extension/Folders/folders.php",
"src/Util/util.php"
您可以使用模板文件和 str_replace
函数,然后像这样操作。
假设您有 style.css
,其中包含:
:root {
--header-bg: #004d73;
--header-title-color: #dbebfa;
}
你有 template.style.css
其中包含:
:root {
--header-bg: {{background}};
--header-title-color: {{color}};
}
现在,只要您需要更改 style.css,您只需替换 template.style.css
中的值并将内容放入 style.css
<?php
$background = $_POST['background'];
$color = $_POST['color'];
// you may need to check these values before using them
$contents = file_get_contents('template.style.css');
$css = str_replace('{{background}}',$background,$contents);
$css = str_replace('{{color}}',$color,$css);
file_put_contents('style.css',$css);
?>
更新
根据您的评论,我了解到您正在尝试为不同服务器上的每个网站使用不同的主题颜色,而您不需要 php。
最简单的方法是为每个网站手动更改每个文件,因为您不会再次更改它们,但是!如果你想要更有活力的东西,你仍然不需要 php,因为在那个原因下你将需要每个网站 Less.js: using 2 variables in your less file (you don't need to change CSS, you only need to replace the colors by variable name @varName
) and set the variable via less.js。
更新
在你的情况下,我认为第一个答案是最好的,我会留下我的第二个建议,以防有人正在寻找那个答案或发现它在不同的情况下有用。
我有一个小 CSS 文件。
:root {
--header-bg:
#004d73;
--header-title-color:
#dbebfa;
}
我正在尝试编写一个接受输入值并用新数据覆盖CSS 文件中的特定行 的脚本。这是我目前所拥有的。
$path = 'custom-test.css';
if (isset($_POST['header-bg'])) {
$fh = fopen($path,"a+");
$string = $_POST['header-bg'];
fwrite($fh,$string,7); // Write information to the file
fclose($fh); // Close the file
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
这是可行的,当然它只是将数据附加到文件末尾。
我不知道是否需要通过 fopen、fwrite 指定要覆盖的行,或者我是否需要脚本将整个文件打开到数组中(即 way 超出了我的基本 PHP 技能)。
同样,我的技能很有限。欢迎所有提示,但扩展我现有代码的建议将非常有帮助。
顺便说一句,我不能为此使用数据库条目。
最好的标准方法是制作一个模板文件,然后使用参数渲染该文件。
看看this
首先制作一个这样的模板文件:
:root {
--header-bg: <?=$this->e($headerBg)?>;
--header-title-color: <?=$this->e($headerTitleColor)?>;
}
并在您的 php 脚本中:
$path = './';
$template = 'custom-test.css';
$output = 'output-test.css';
if (isset($_POST['header-bg'])) {
$params = [
'headerBg' => $_POST['header-bg'],
'headerTitleColor' => ''
];
$templates = new League\Plates\Engine($path);
file_put_content($output, $templates->render($template, $params));
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
您必须使用 composer 安装此软件包(如果您使用的是 composer):
composer require league/plates
然后在你的脚本中导入它require 'vendor/autoload.php'
如果您不使用作曲家,则必须 download it 并在脚本中包含以下文件
"src/Template/match.php",
"src/Extension/Data/data.php",
"src/Extension/Path/path.php",
"src/Extension/RenderContext/func.php",
"src/Extension/RenderContext/render-context.php",
"src/Extension/LayoutSections/layout-sections.php",
"src/Extension/Folders/folders.php",
"src/Util/util.php"
您可以使用模板文件和 str_replace
函数,然后像这样操作。
假设您有 style.css
,其中包含:
:root {
--header-bg: #004d73;
--header-title-color: #dbebfa;
}
你有 template.style.css
其中包含:
:root {
--header-bg: {{background}};
--header-title-color: {{color}};
}
现在,只要您需要更改 style.css,您只需替换 template.style.css
中的值并将内容放入 style.css
<?php
$background = $_POST['background'];
$color = $_POST['color'];
// you may need to check these values before using them
$contents = file_get_contents('template.style.css');
$css = str_replace('{{background}}',$background,$contents);
$css = str_replace('{{color}}',$color,$css);
file_put_contents('style.css',$css);
?>
更新
根据您的评论,我了解到您正在尝试为不同服务器上的每个网站使用不同的主题颜色,而您不需要 php。
最简单的方法是为每个网站手动更改每个文件,因为您不会再次更改它们,但是!如果你想要更有活力的东西,你仍然不需要 php,因为在那个原因下你将需要每个网站 Less.js: using 2 variables in your less file (you don't need to change CSS, you only need to replace the colors by variable name @varName
) and set the variable via less.js。
更新
在你的情况下,我认为第一个答案是最好的,我会留下我的第二个建议,以防有人正在寻找那个答案或发现它在不同的情况下有用。