Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于CSS选择器的正则表达式-PHP_Php_Css_Regex - Fatal编程技术网

用于CSS选择器的正则表达式-PHP

用于CSS选择器的正则表达式-PHP,php,css,regex,Php,Css,Regex,有人知道从css声明中选择所有选择器的最健壮的正则表达式是什么吗?下面是非标准CSS的CSS代码,这将是一个很好的测试用例 .somenormalstyledeclaration { width:100%; height:100%; background-image: url(images/fallback-gradient.png); } .somenormalstyle1, .somenormalstyle2 { width:100%; heigh

有人知道从css声明中选择所有选择器的最健壮的正则表达式是什么吗?下面是非标准CSS的CSS代码,这将是一个很好的测试用例

.somenormalstyledeclaration 
{
    width:100%;
    height:100%;
    background-image: url(images/fallback-gradient.png);
}
.somenormalstyle1, .somenormalstyle2 
{
    width:100%;
    height:100%;
    background-image: url(images/fallback-gradient.png);
}
.gradient-bg 
{
   background-color: #1a82f7;
   background-image: url(images/fallback-gradient.png);
   background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#2F2727), to(#1a82f7));
   background-image: -webkit-linear-gradient(top, #2F2727, #1a82f7);
   background-image:    -moz-linear-gradient(top, #2F2727, #1a82f7);
   background-image:     -ms-linear-gradient(top, #2F2727, #1a82f7);
   background-image:      -o-linear-gradient(top, #2F2727, #1a82f7);
}

@-webkit-keyframes bounce_circle 
{
    0% 
    {
        opacity:0.3;
    }
    50% 
    {
        opacity:1;
        background-color:#111
    }
    100% 
    {
        opacity:0.3;
    }
}
这应该最有效。这只需要几个步骤,但不如完整的解析器好

用空格字符替换所有换行符 假设从不使用包含{或}的字符串,可以用, 将所有文本拆分为每个文本上的数组,然后;性格 从每个选择器的开头和结尾修剪所有空白 数组的每个元素都应该是选择器或at规则,即@charset utf-8


我可能错过了一些其他的边缘案例,但对于大多数案例来说,这应该是一个快速而肮脏的解决方案。

虽然这不适用于@-webkit。。。它只需要ID和类选择器

我相信正则表达式向导会更有效率

他帮我指路。尽管它是javascript,我还是能够理解要点

// Snag file
$text = file_get_contents("file.css");

// Strip comments
$text = preg_replace('!/\*.*?\*/!s', '', $text);

// Clear out everything between brackets
$text = preg_replace('/{(.*?)}/si', '', $text);

// Get ID and class selectors
$pattern = '/([#|\.])([_a-z]+[_a-z0-9-]*)/mi';

// Array to store results.
$result = array();

// No "g' modifier in PHP, use preg_match_all for this kind of behaviour.
preg_match_all($pattern, $text, $result);

print "<pre>";
print_r ( $result );
print "</pre>";

解析器会更加健壮,尤其是嵌套大括号的解析器!cssmin看起来很有前途。现在开始测试它…啊,看起来它在解析@-webkit关键帧声明时有一个bug。我刚刚向作者提交了一个bug。