Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Find all CSS classes in Stylesheet Using PHP

Featured Replies

Hey guys,

 

Seriously hope someone can help me out here. I'm tearing my hair out!

 

So I'm trying to create a dynamic page which creates "squares" and applies a CSS class to them based on what is in a stylesheet.

Sounds simple... however the page needs to auto update when it's loaded and finds a new CSS class within the said stylesheet.

Said example can be found here: http://staff.djep.co/styleguide.php

 

 

In essense, I need to find a way to use PHP to read through a CSS file and create the boxes in a "for each" loop.

I'm able to write the for each loop...blah blah blah... but I'm stuck when I get to this sort of CSS in the stylesheet:

.metro-block-plum-faded, .metro-button-plum-faded {
background-color: #AC859A !important; 
color: white !important;  
}
.metro-button-plum-faded:hover {
background-color: #7F566D !important; 
color: white !important;  
}
.metro-block-mocha, .metro-button-mocha {
background-color: #564D41 !important; 
color: white !important; 
}
.metro-block-statspurple { 
background-color: #6E36CF !important; 
color: white !important;  
}

To start with ... I only need to get the "metro-block" bits .... so the CSS class could be called "metro-block-plum-faded" or it could be "metro-block-mocha".... however they ALWAYS start metro-block ..... I don't need the bit after (metro-button) or the hover CSS.

 

Can anyone tell me how I do this... I'm going insane!


PS: I borrowed some code I found from Stackoverflow but I can't quite get it to work how I want, this is what I'm currently using:

<?php
$css = file_get_contents('http://www.djep2.com/includes/style.css');
preg_match_all( '/(?ims)([a-z0-9\s\,\.\:#_\-@]+)\{([^\}]*)\}/', $css, $arr);
$result = array();
foreach ($arr[0] as $i => $x)
{
    $selector = trim($arr[1][$i]);
    $rules = explode(';', trim($arr[2][$i]));
    $result[$selector] = array();
    foreach ($rules as $strRule)
    {
        if (!empty($strRule))
        {
            $rule = explode(":", $strRule);
            $result[$selector][][trim($rule[0])] = trim($rule[1]);
        }
    }
}   

?>


<div class="col-xs-6">
		<div class="padding-all-10">
			<?php
			foreach ($result as $key => $value) {
				if(".metro-block-" == substr($key,0,13)){
					$number = substr($key,strrpos($key,'-'));
					// do whatever you need to with $number..
					echo '<div class="col-xs-4"><div class="margin-all-10 padding-all-10 metro-block-lightgray" style="border:1px solid #555;"><div class="block metro-block metro-block'.$number.' margin-all-5">';
					echo 'metro-block'.$number;
					echo '</div></div></div>';
				  }
			}
			?>
		</div>
	</div>

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.