I was looking for a function to read css data (string or file).
All the solutions listed here are great!!!
But the function that was most useful to me was the from Gabriel Anderson
I renamed the function and extended it a bit, counting curly brakes and so on.
The function is now able to read files or strings with css content.
Faulty css data generate error messages that are output under the key 'debug-errors-cssreader'.
- If the curly brakes are unequal, there is an error message.
- If it is a file and it doesn't exist then there is an error message.
- If the string empty (string or filename), too.
You can also use a regExp search pattern to filter the output.
$cssStr = 'path/to/file.css'; // or CSS code
$returnSelectorOnly = true; // (optional) return css selector only
$regExpFilter = '/(your regExp)/'; // your (preg_match) pattern
$css = cssReader($cssStr, $returnSelectorOnly, $regExpFilter);
I have also extended it so that you can only display the selectors in an array.
I use the function, for example, to read out icofont/fontawesome css files, I only output the css selectors and process them further.
I have therefore programmed overview pages to see the icons that I have available.
Here a little example
$css = cssReader('icofont.css', true, '/(.icofont-)(.*?)(:before)/');
echo "<pre>";
echo var_dump($css);
echo "</pre>";
Output
array(2105) {
[0]=>
string(29) ".icofont-angry-monster:before"
[1]=>
string(23) ".icofont-bathtub:before"
[2]=>
string(26) ".icofont-bird-wings:before"
[3]=>
string(19) ".icofont-bow:before"
[4]=>
string(25) ".icofont-brain-alt:before"
[5]=>
string(29) ".icofont-butterfly-alt:before"
[6]=>
string(22) ".icofont-castle:before"
[7]=>
string(23) ".icofont-circuit:before"
[8]=>
string(20) ".icofont-dart:before"
[9]=>
string(24) ".icofont-dice-alt:before"
...
}
Maybe someone else needs the function just like me, below I put the entire function in including the example.
Thanks again for the great function!
function cssReader($cssStr, $returnSelectorOnly = false, $regExpFilter = "") {
$css = false;
$result = array();
$error = array();
$debug = true;
$isfile = false;
$filename = @trim(@pathinfo($cssStr)['filename']);
if ($cssStr != "" && $filename != "") {
$isfile = true;
}
// checking for is file and file exists
if (is_file($cssStr)) {
$cssStr = file_get_contents($cssStr);
$countCurlyBrakes_open = substr_count($cssStr, "{");
$countCurlyBrakes_close = substr_count($cssStr, "}");
if ($countCurlyBrakes_open && $countCurlyBrakes_close) {
if ($countCurlyBrakes_open == $countCurlyBrakes_close) {
$css = $cssStr;
} else {
// debug
$error[] = "#1 File error: The counting of '{' or '}' was different, '{' = ".$countCurlyBrakes_open." and '}' = ".$countCurlyBrakes_close.".";
}
} else {
// debug
$error[] = "#2 File error: Curly braces error, the counting of '{' or '}' was 0 (zero).";
}
} else {
if ($isfile) {
// debug
$error[] = "#3 File error: '".$cssStr."' the file does not exist.";
}
}
// checking for is not a file and has no file extension and is shorter than 2049 characters
// !!! // Technically speaking, your URL should never be longer than 2,048 characters. Any long than this and Internet Explorer won’t be able to load your page
if (!$isfile) {
if (!empty($cssStr)) {
$countCurlyBrakes_open = substr_count($cssStr, "{");
$countCurlyBrakes_close = substr_count($cssStr, "}");
if ($countCurlyBrakes_open && $countCurlyBrakes_close) {
if ($countCurlyBrakes_open == $countCurlyBrakes_close) {
$css = $cssStr;
} else {
// debug
$error[] = "#4 String error: The counting of '{' or '}' was different, '{' = ".$countCurlyBrakes_open." and '}' = ".$countCurlyBrakes_close.".";
}
} else {
// debug
$error[] = "#5 String error: Curly braces error, the counting of '{' or '}' was 0 (zero).";
}
} else {
// debug
$error[] = "#6 String error: (string) $cssStr was empty.";
}
}
// place errors on top of the array
if ($debug && count($error)) {
$result['debug-errors-cssreader'] = $error;
}
$regExp = "/(?ims)([a-z0-9\s\.\:#_\-@,]+)\{([^\}]*)\}/";
preg_match_all(''.$regExp.'', $css, $arr);
foreach ($arr[0] as $i => $x){
$selector = trim($arr[1][$i]);
if ($returnSelectorOnly == false || $returnSelectorOnly == "" || !$returnSelectorOnly) {
$rules = explode(';', trim($arr[2][$i]));
$rules_arr = array();
foreach ($rules as $strRule){
if (!empty($strRule)){
$rule = explode(":", $strRule);
$rules_arr[trim($rule[0])] = trim($rule[1]);
}
}
}
$selectors = explode(',', trim($selector));
foreach ($selectors as $strSel){
if ($returnSelectorOnly == false || $returnSelectorOnly == "" || !$returnSelectorOnly) {
$result[$strSel] = $rules_arr;
} else {
if ($regExpFilter == false || $regExpFilter == "" || !$regExpFilter) {
$result[] = $strSel;
} else {
if (preg_match($regExpFilter, $strSel)) {
$result[] = $strSel;
}
}
}
}
}
return $result;
}
$css = cssReader('icofont.css', true, '/(.icofont-)(.*?)(:before)/');
echo "<pre>";
echo var_dump($css);
echo "</pre>";