gpt4 book ai didi

php - 使用 PHP 如何找到 CSS 标签/id/类并替换大括号内的样式?

转载 作者:行者123 更新时间:2023-11-28 13:41:38 25 4
gpt4 key购买 nike

我需要遍历 CSS 文件以查找特定元素并将其 CSS 更改为主题编辑器的更新内容。我在大括号之间有一个包含所有 CSS 的变量,我只需要找到一种方法来选择给定标签后的大括号并替换内容。任何帮助将不胜感激!谢谢

最佳答案

也许不是唯一的解决方案,但您是否考虑过将 CSS 放入数据库并即时生成 CSS 文件?

将它存储在数据库中还使您可以在每次发生更改时重新生成文件并像这样缓存它以节省性能。

要修改现有文件,您可能需要遍历该文件,并希望没有人手动更改您的 CSS 文件的样式约定,并通过记住最后一个标签/类/id 表达式(将可能无法开箱即用!):

$newFileContent = "";
$searchClass = "td.content";
$lines = file("style.css");
$insideSearchedTag = false;
foreach($lines as $line) {
if (strstr($line, $searchClass) !== false) {
$insideSearchedTag = true;
$newFileContent .= "\n" . $line;
}
else if (strstr($line, "}") !== false) {
$insideSearchedTag = false;
$newFileContent .= "\n" . $line;
}
else if ($insideSearchedTag) {
// search/replace the content you want to replace.
$newLineContent = str_ireplace($searchStyle, $replaceStyle, $line);
$newFileContent .= "\n" . $newLineContent;
}
else { $newFileContent .= "\n" . $line; }
}
fwrite($file, $newFileContent);

关于php - 使用 PHP 如何找到 CSS 标签/id/类并替换大括号内的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12317214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com