I thought it would be pretty simple, but right now I'm struggling a bit with that. I know that there are CSS parser classes that can accomplish what I want to do ... but I don’t need 95% of the functionality that they have, so they are not really doable and will just be too heavy.
All I have to do is pull out any class names and / or identifiers used in the CSS file with a regular expression. Here the regex that I thought would work, but doesn't have.
[^a-z0-9][\w]*(?=\s)
When running against my sample:
.stuffclass {
color:#fff;
background:url('blah.jpg');
}
.newclass{
color:#fff;
background:url('blah.jpg');
}
.oldclass {
color:#fff;
background:url('blah.jpg');
}
#blah.newclass {
color:#fff;
background:url('blah.jpg');
}
.oldclass#blah{
color:#fff;
background:url('blah.jpg');
}
.oldclass #blah {
color:#fff;
background:url('blah.jpg');
}
.oldclass .newclass {
text-shadow:1px 1px 0 #fff;
color:#fff;
background:url('blah.jpg');
}
.oldclass:hover{
color:#fff;
background:url('blah.jpg');
}
.newclass:active {
text-shadow:1px 1px 0 #000;
}
, , . . , #blah.newclass : #blah AND .newclass.
?
===================
2 , { }, .
:
$file = file_get_contents('css/style.css');
$pattern_one = '/(?<=\{)(.*?)(?=\})/s';
$pattern_two = '/[\.|#][\w]([:\w]+?)+/';
$stripped = preg_replace($pattern_one, '', $file);
$selectors = array();
$matches = preg_match_all($pattern_two, $stripped, $selectors);
print_r(array_unique($selectors[0]));