Read CSS files with PHP
This function will open CSS file read it’s styles and separate selectors from property value pairs. The function stores the values in two array sessions (names and styles). The values can be then easily retrieved using loops or if/else statements.
PHP functions used
file – Reads entire file into an array
trim – Strip whitespace (or other characters) from the beginning and end of a string
This function returns a string with whitespace stripped from the beginning and end of str.
strtok – Tokenize string
strtok() splits a string (str) into smaller strings (tokens), with each token being delimited by any character from token. That is, if you have a string like “This is an example string” you could tokenize this string into its individual words by using the space character as the token.
strstr – Find first occurrence of a string
Returns part of haystack string from the first occurrence of needle to the end of haystack.
count - Count elements in an array, or properties in an object
Returns the number of elements in var, which is typically an array, since anything else will have one element.
PHP % operator
I will be using Modulus (%) operator which returns a remainder of $a divided by $b.
Sample CSS
I am going to use this simple CSS file (style_sheet.css) to help me explain the PHP code
p{
color:#000000;
}
h1{
font-size:18px;
color:#666666;
}
table{
width:100%;
border:1px solid #000000;
}
PHP code
For those of you who actually want to know how the code works, please read the comments.
//if no style loaded load
$lines = file("style_sheet.css");
foreach ($lines as $line_num => $line) {
$cssstyles .= trim($line);
}
//using strtok we remove the brackets around the css styles
$tok = strtok($cssstyles, "{}");
//For example, the style: p{color:#000000;} now looks like this p color:#000000;
//crete another array in which we will store tokenized string
$sarray = array();
//set counter
$spos = 0;
//with this while loop we are basically separating selectors from styles and store those values in the $sarray
while ($tok !== false) {
$sarray[$spos] = $tok;
$spos++;
$tok = strtok("{}");
}
//if you run print_r($sarray); the result would be:
//Array ( [0] => p [1] => color:#000000;
// [2] => h1 [3] => font-size:18px;color:#666666;
// [4] => table [5] => width:100%; border:1px solid #000000;)
//As you can see all selectors are stored in odd number positions of the array and styles in even.
//That is an important piece of information that we will use to to go through $sarray and store
//all selectors in one array and styles in the other.
// To start we need to get the size of $sarray
$size = count($sarray);
//create selectors and styles arrays
$selectors = array();
$sstyles = array();
//set counters
$npos = 0;
$sstl = 0;
//a simple for loop with modulus operator will help us separate styles from selectors.
for($i = 0; $i<$size; $i++){
if ($i % 2 == 0) {
$selectors[$npos] = $sarray[$i];
$npos++;
}else{
$sstyles[$sstl] = $sarray[$i];
$sstl++;
}
}
//all that's left is store names and styles in a session for us to use
$_SESSION['style_names'] = $selectors;
$_SESSION['style_styles'] = $sstyles;
//now if you run print_r($selectors); and print_r($sstyles);
//you'll notice that you can access individual selectors and it's styles using array keys
//In this example $selectors[0] value is "p" and $sstyles[0] value is "p" property/value pair-color:#000000;

shuvro
I am reading your scripts. those are so much favorable and helpful to learn. I am showing my gratitude to you from Bangladesh.
yiqing95
great work , learn some useful skills , thanks !
Selase
I have read through several of your blogs and i like them for their simple layout and how easy it is to understand even as a newbie. I will like to share on this particular topic, scenarios or conditions under which it will be useful to split a css file. I really want to know. Thank you
eplicanic
You could use it to create a GUI CSS editor for a CMS