0

我有一个大的 .css 文件,它都是内联编码的。这使得准确地找到元素变得非常不优雅和困难。我想将它粘贴到一个 PHP 函数中,该函数在每个“{”括号后给出一个空格,因为如果我手动执行它会花费我更多时间。

我应该使用哪个功能?

4

3 回答 3

1
$input_data = str_replace('}', '} ', $input_data);
于 2019-09-06T10:52:33.640 回答
0

你可以preg_replace在这里使用:

$input = "p.a {
        font: 15px arial, sans-serif;
    }";
$output = preg_replace("/\{/", " {", $input);
echo $input . "\n";
echo $output;

这打印:

p.a {
        font: 15px arial, sans-serif;
    }
p.a  {    <-- extra space here
        font: 15px arial, sans-serif;
    }
于 2019-09-06T10:05:24.043 回答
0

如果您唯一的兴趣是在所有“{”之后添加一个空格,则 toy 可以轻松使用以下代码:

$CSS = '.sample{
    padding: 10px;
}';
echo 'Old CSS code: '.$CSS.PHP_EOL.PHP_EOL;

$cleanCSS = str_replace('{', '{ ', $css);
echo 'Clean CSS code: '.$cleanCSS;

此代码打印:

Old CSS code: .sample{
    padding: 10px;
}

Clean CSS code: .sample {
    padding: 10px;
}
于 2019-09-06T10:07:44.557 回答