我需要一些帮助从 CSS 创建多维数组。例如:
$css = "body { font-weight:40px; color:#444; } #wrapper { width: 500px; }";
我确实有来自 Split CSS inside the {} to array of key/value pairs 的以下代码但这也不适用于选择器
function BreakCSS($CSS) {
$results = array();
foreach(explode(';', $CSS) AS $attr)
if (strlen(trim($attr)) > 0) // for missing semicolon on last element, which is legal
{
list($name, $value) = explode(':', $attr);
$results[trim($name)] = trim($value);
}
return $results;
我不是这种编码方面的专家。理想情况下,我想:
array(
'body' => array (
array(
'font-weight',
'40',
'px'
),
array(
'color',
'#444',
''
)
)
)
此外,有没有办法检查最后是否添加了!important?
提前致谢!
最佳答案
更新!我找到了一个解决方案,有人修改了上面最初添加的代码。 Break a CSS file into an array with PHP
不是 100% 完美(建议使用解析器),但这适用于我的快速测试或概念验证。
关于php - 将 CSS 拆分为多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45098156/