PHP CSS解析器 - 选择器声明到字符串 [英] PHP CSS Parser - Selector Declarations to String

查看:134
本文介绍了PHP CSS解析器 - 选择器声明到字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要能够读取一个CSS文件,并能够提取给定选择器的所有声明到一个字符串。例如,给定以下样式表:

I want to be able to read a CSS file, and be able to extract all declarations of a given selector in to a string. For example, given the following stylesheet:

h1 {
  font-size: 15px;
  font-weight: bold;
  font-style: italic;
  font-family: Verdana, Arial, Helvetica, sans-serif;
}

div.item {
  font-size: 12px;
  border:1px solid #EEE;
}



我想要能够调用并获得div.item,

I want to be able to call and get div.item, something like:

$css->getSelector('div.item');

这应该给我一个字符串:

Which should give me a string like:

font-size:12px;border:1px solid #EEE;

我一直在找,但找不到一个可以做到这一点的解析器。任何想法?

I have been looking around but can't find a parser that can do exactly that. Any ideas?

FYI:我需要这个能够从CSS转换选择器,并动态地嵌入到电子邮件中的HTML元素中。

FYI: I need this to be able to convert selectors from a CSS and embed the styles dynamically in to HTML elements in email messages.

解决方案
编辑:我想出了自己的粗解决方案,并创建了一个类来做我想要的。

SOLUTION I came up with my own crude solution and created a class to do what I was looking for. See my own answer below.

推荐答案

我想出了自己的粗解决方案,并创建了一个类来做我想要的。我的来源在底部被引用。

I came up with my own crude solution and created a class to do what I was looking for. My sources are referenced at the bottom.

class css2string {
    var $css;

    function parseStr($string) {
        preg_match_all( '/(?ims)([a-z0-9, \s\.\:#_\-@]+)\{([^\}]*)\}/', $string, $arr);
        $this->css = array();
        foreach ($arr[0] as $i => $x)
        {
            $selector = trim($arr[1][$i]);
            $rules = explode(';', trim($arr[2][$i]));
            $this->css[$selector] = array();
            foreach ($rules as $strRule)
            {
                if (!empty($strRule))
                {
                    $rule = explode(":", $strRule);
                    $this->css[$selector][trim($rule[0])] = trim($rule[1]);
                }
            }
        }
    }

    function arrayImplode($glue,$separator,$array) {
        if (!is_array($array)) return $array;
        $styleString = array();
        foreach ($array as $key => $val) {
            if (is_array($val))
                $val = implode(',',$val);
            $styleString[] = "{$key}{$glue}{$val}";

        }
        return implode($separator,$styleString);   
    }

    function getSelector($selectorName) {
        return $this->arrayImplode(":",";",$this->css[$selectorName]);
    }

}

$cssString = "
h1 {
  font-size: 15px;
  font-weight: bold;
  font-style: italic;
  font-family: Verdana, Arial, Helvetica, sans-serif;
}

div.item {
  font-size: 12px;
  border:1px solid #EEE;
}";

$getStyle = new css2string();
$getStyle->parseStr(cssString);
echo $getStyle->getSelector("div.item");

输出如下:

font-size:12px;border:1px solid #EEE

参考文献:
http://www.php.net/manual/en/function.implode.php#106085
http://stackoverflow.com/questions/1215074/break-a-css-file-into-an-array-with-php

References: http://www.php.net/manual/en/function.implode.php#106085 http://stackoverflow.com/questions/1215074/break-a-css-file-into-an-array-with-php

这篇关于PHP CSS解析器 - 选择器声明到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆