【问题标题】:PHP CSS from string to arrayPHP CSS从字符串到数组
【发布时间】:2016-02-06 11:29:07
【问题描述】:

不知何故,这一定很简单,但我想不通,而且我已经做了一整天了。

我想将一个 CSS 文件解析成一个包含如下键和值的数组:

Array('#idname' => Array('overflow' => hidden, 'color' => '#FFF'));

我通过使用正则表达式删除所有媒体查询并删除所有空格来忽略所有媒体查询。

//Remove all media queries
$cssFromLink = preg_replace("/@media.*?}}/i", '', $cssFromLink);
//Remove all whitespace
$cssFromLink = str_replace(' ','', $cssFromLink);

我想要的只是能够在列表中搜索 id 或类名,然后提取像 background-color 这样的属性。

像 Sabberworm 和其他 CSS 解析器这样的库似乎对我不起作用,它们要么似乎永远/什么都不做,要么抛出一个致命错误。我正在 apple.com 的 css 上尝试这个。

在我看来,所有其他解决方案都同样复杂,但几乎没有一个特别适用于 apple.com,而且我不能让它在流行网站上崩溃。

【问题讨论】:

标签: php css parsing


【解决方案1】:

来自 Parse a CSS file with PHP 的 JapanPro 的回答最适合我。它仍然有一些错误(一个 } 在一些 id 前面),我不确定使用正则表达式是否是在每种情况下解析它的最佳方法,但现在我将使用它。

<?php

$css = <<<CSS
#selector { display:block; width:100px; }
#selector a { float:left; text-decoration:none }
CSS;

//
function BreakCSS($css)
{

    $results = array();

    preg_match_all('/(.+?)\s?\{\s?(.+?)\s?\}/', $css, $matches);
    foreach($matches[0] AS $i=>$original)
        foreach(explode(';', $matches[2][$i]) AS $attr)
                if (strlen($attr) > 0) // for missing semicolon on last element, which is legal
                {
                        // Explode on the CSS attributes defined
                        list($name, $value) = explode(':', $attr);
                        $results[$matches[1][$i]][trim($name)] = trim($value);
                }
    return $results;
}
var_dump(BreakCSS($css));

【讨论】:

【解决方案2】:

我刚做了这个,试试看:

<?php

    //To test
    $string = "#id {
        overflow: hidden;
        color: #fff;
    }
    #id2 {
        margin: 0px;
        height: 100%;
    }";

    //Call the function and print it out
    $css_array = cssToArray($string);
    echo "<pre>";
    print_r($css_array);

    //The actual function
    function cssToArray($css){
        //Regex to find tags and their rules
        $re = "/(.+)\{([^\}]*)\}/";
        preg_match_all($re, $css, $matches);

        //Create an array to hold the returned values
        $return = array();
        for($i = 0; $i<count($matches[0]); $i++){
            //Get the ID/class
            $name = trim($matches[1][$i]);

            //Get the rules
            $rules = trim($matches[2][$i]);

            //Format rules into array
            $rules_a = array();
            $rules_x = explode(";", $rules);
            foreach($rules_x as $r){
                if(trim($r)!=""){
                    $s = explode(":", $r);
                    $rules_a[trim($s[0])] = trim($s[1]);
                }
            }

            //Add the name and its values to the array
            $return[$name] = $rules_a;
        }

        //Return the array
        return $return;
    }

【讨论】:

  • 至少这样做了。但结果是这样的s14.postimg.org/uabenw735/resultcss.png
  • 那是因为你对每组的最后一条规则没有最后的;
  • 如果 all 您的 CSS 规则都是这种情况,请执行查找/替换,将“}”替换为“;}”,它应该可以工作
  • 这是一个有效的语法,对吧?但是我用 ;} 替换了所有 } 来测试它,它仍然给出相同的输出。我自己的想法是循环遍历字符串,然后将 {} 引号内的所有内容与 id/class 匹配,因为对我来说规则是什么并不重要。
  • 您能否将您的 CSS 代码发布到 PasteBin 并在此处发布?
猜你喜欢
  • 2011-11-21
  • 1970-01-01
  • 2016-01-09
  • 1970-01-01
  • 2017-04-15
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多