php - 将 CSS 拆分为多维数组

标签 php css arrays multidimensional-array

我需要一些帮助从 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/

相关文章:

javascript - 为什么这个下拉菜单在 IE 中不起作用?

css - 如何使div在固定位置水平居中?

javascript - 将数组从一种格式转换为另一种格式时出现问题

java - 为什么我的程序不输出数字

PHP in_array 函数,为什么这不起作用?

php - 需要 future 7 天内包含日期的所有 MySQL 行,但日期列的格式不正常

php - 静默/隐藏的 GET 请求是否可能?

php - mysql匹配值,找到相似并排序,然后按原始匹配值对排序后的组进行排序

php - Php动态嵌入YouTube视频

css - 溢出-x : hidden not work well in Chrome with a child DIV content that equals or exceeds the parent size