Extract css classes and id from source using php

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 , { }, .

:

//Grab contents of css file
$file = file_get_contents('css/style.css');

//Strip out everything between { and }
$pattern_one = '/(?<=\{)(.*?)(?=\})/s';

//Match any and all selectors (and pseudos)
$pattern_two = '/[\.|#][\w]([:\w]+?)+/';

//Run the first regex pattern on the input
$stripped = preg_replace($pattern_one, '', $file);

//Variable to hold results
$selectors = array();

//Run the second regex pattern on $stripped input
$matches = preg_match_all($pattern_two, $stripped, $selectors);

//Show the results
print_r(array_unique($selectors[0]));
+3
3
[^a-z0-9][\w]+(?=\s)

* +

RegexR - awesome : http://gskinner.com/RegExr/ (. )

+1
/(?<!:\s)[#.][\w]*/

- ? #FFFFFF...

0

The solution posted by the OP works, although it does not work for me with hyphenated CSS classes. So I modified the second template to work more efficiently:

$pattern_two = '/[\.|#]([A-Za-z0-9_\-])*(\s?)+/';
0
source

All Articles