Return CSS file classes using Regex and PHP

1

I have the following CSS:

.icon-a{
    background:black;
    color:white;
}    
.icon-b{
    backgroundwhite;
    color:black;
}    
.icon-c{
    background:blue;
    color:yellow;
}

I wanted a PHP script that could read the CSS file and return only the selector classes in an array:
Ex:

Array(
    0 => "icon-a",
    1 => "icon-b",
    2 => "icon-c"
 )
    
asked by anonymous 09.11.2015 / 14:58

1 answer

1

You can use this regular expression, \.[\w-]+ a dot by following a letter / number [a-zA-Z0-9] a stroke by combining more than once, modifying m makes the combination work on multi- / p>

<?php

$css  ='.icon-a{
    background:black;
    color:white;
}    
.icon-b{
    backgroundwhite;
    color:black;
}    
.icon-c{
    background:blue;
    color:yellow;
}';


preg_match_all('#\.[\w-]+#m', $css, $m);

echo "<pre>";
print_r($m);
    
09.11.2015 / 15:15