0

我有一个包含以下内容的 css 文件:

#firstdiv { width:100%; height:200px; }
#seconddiv { width:80%; height:70px; }
#thirddiv { width:80%; height:70px; }
#firstdiv { color:red; background:yellow; }
#seconddiv {  color:red; background:green; }
#firstdiv { border:3px solid black; border-rdius:5px; }

如何#firstdiv使用 php 删除所有 css 属性?

这是我想要的输出:

#seconddiv { width:80%; height:70px; }
#thirddiv { width:80%; height:70px; }
#seconddiv {  color:red; background:green; }
4

1 回答 1

0

最简单的方法是根据换行符拆分文件,然后检查每一行是否以您不想要的字符串开头,最后将文件保存到该位置。

$filelocation = "/path/to/file"; //please update for your situation
$csscontents = file_get_contents($filelocation);
$lines = explode(PHP_EOL,$csscontents);
$csscontents = '';
foreach($lines as $line) {
    if (substr($line,0,9) !== "#firstdiv") $csscontents .= $line . PHP_EOL;
}
file_put_contents($filelocation,$csscontents);

如果一行中有多个选择器,则需要执行此操作

$filelocation = "/path/to/file"; //please update for your situation
$csscontents = file_get_contents($filelocation);
$lines = explode('}',$csscontents);
$csscontents = '';
foreach($lines as $line) {
    if (substr(preg_replace('/\s+/', '',$line),0,9) !== "#firstdiv" AND !empty($line) AND !ctype_space($line)) $csscontents .= $line . "}";
}
file_put_contents($filelocation,$csscontents);
于 2016-12-30T17:15:43.903 回答