28, May 2012

using PHP to extract CSS variables - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: using PHP to extract CSS variables
« previous next »
Pages: [1] Print
Instabuck - The easy way to sell digital products online

Author Topic: using PHP to extract CSS variables  (Read 1867 times)
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 886
1148 credits
Members referred : 4



« on: Jul 11, 2007, 09:36:35 am »

is there anyway with PHP to extract variables from a CSS stylesheet? hypothetically, like so:

say we have some stylesheet, style.css, here is the content:

Code:
.tableClass { background-color: #ffffff; }

and then we have a php file that calls upon this stylesheet in the header, but somehow extras that background-color value from the stylesheet? is this possible? or what I am asking for is too much of PHP  Cool


Last blog : phpHaze 1.59.1 in Development
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #1 on: Jul 11, 2007, 10:02:27 am »

Of course it is possible but you need to create a parser for this which is not an easy task to do Smiley

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 886
1148 credits
Members referred : 4



« Reply #2 on: Jul 11, 2007, 10:14:45 pm »

would this work?

use file_get_contents to load the data from the CSS file, then use strpos and strstr and etc to find the values im looking for?


Last blog : phpHaze 1.59.1 in Development
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #3 on: Jul 11, 2007, 10:19:51 pm »

I guess that would work, but remember that you can't do this on each pageview because it will cause a lot of overhead in the server Smiley

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 886
1148 credits
Members referred : 4



« Reply #4 on: Jul 12, 2007, 08:58:59 am »

i suppose that is because of file_get_contents..hmm i will have to play around with it some and see how I can get this css file something I can work with in PHP Cool


Last blog : phpHaze 1.59.1 in Development
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 886
1148 credits
Members referred : 4



« Reply #5 on: Jul 12, 2007, 04:02:22 pm »

another idea, maybe a bit noobish, but tell me if this is a better way..

just create a new DB table, and run this script with file_get_contents, and load the necessary values once at the first script execution, to grab the values needed, then store them in the DB. when needed, just call them from the DB as usual. if style file is changed, re-run the file_get script..


Last blog : phpHaze 1.59.1 in Development
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #6 on: Jul 12, 2007, 04:13:31 pm »

First, better tell us what exactly do you want to do?

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #7 on: Jul 12, 2007, 07:34:45 pm »

yeah tell us your secrets Smiley

aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 886
1148 credits
Members referred : 4



« Reply #8 on: Jul 12, 2007, 07:57:49 pm »

well basically, this script should get this value from the CSS file because the themes are separate from the actual CMS that I am modifying for a client, and each theme and CSS value for this class we are looking up is unique. so, he wants the table cells when hovered to change color (like some forum themes for phpBB and etc). simple to do, with HTML, but it should match each theme used, even if the theme is changed, so the value for the colors will also change. in this case, we need php to get these values on-the-fly. i would rather not use a DB to store the data, i'd like to to be on each page view, but with file_get_contents you say this creates too much overhead


Last blog : phpHaze 1.59.1 in Development
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #9 on: Jul 12, 2007, 09:32:02 pm »

maybe you can use inline styles plus dyn. values instead

aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 886
1148 credits
Members referred : 4



« Reply #10 on: Jul 12, 2007, 09:59:38 pm »

maybe you can use inline styles plus dyn. values instead

you mean like adding the styles directly to the PHP page? no this wont work, its not a CMS im developing, this is a CMS that already exists.. the theme has each style file, i have a solution now that works but it requires file_get_contents to load the CSS code into a string


Last blog : phpHaze 1.59.1 in Development
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #11 on: Jul 12, 2007, 10:15:02 pm »

and what if you echo diff. values in the header from the file?

aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 886
1148 credits
Members referred : 4



« Reply #12 on: Jul 12, 2007, 10:28:05 pm »

heres a better example, this is our HTML page header:

Code:
<html>
<head>
<title>".xhtmlValid($settings['sitename']." - ".$ptitle)."</title>
<meta http-equiv='Content-Type' content='text/html; charset=".$locale['charset']."' />
<meta name='description' content='".$settings['description']."' />
<meta name='keywords' content='".$settings['keywords']."' />
<link rel='stylesheet' href='".THEME."styles.css' type='text/css' />

this is the stylesheet we are referring to, styles.css, and as you see by the used definition THEME, it varies depending on site settings and user preference. therefore, the color for the following has to be dynamic based on the theme in question:

$mouseout1 " onmouseover=\"this.style.backgroundColor='".$class['tbl2']."'\" onmouseout=\"this.style.backgroundColor='".$class['tbl1']."'\"";
$mouseout2 " onmouseover=\"this.style.backgroundColor='".$class['tbl1']."'\" onmouseout=\"this.style.backgroundColor='".$class['tbl2']."'\"";
?>


as you expect, the variables above are just added to table cell tags and this makes the background switch back and forth based on the themes colors in the CSS File, what i intended to do, was this:

<?php
//function.strBetween: Returns a string found between 2 strings, in a string
function strBetween($s1,$s2,$s){
	
$s1 strtolower($s1);
	
$s2 strtolower($s2);
	
$L1 strlen($s1);
	
$scheck strtolower($s);
	
if(
$L1>0){$pos1 strpos($scheck,$s1);} else {$pos1=0;}
	
if(
$pos1 !== false){
	
	
if(
$s2 == '') return substr($s,$pos1+$L1);
	
	
$pos2 strpos(substr($scheck,$pos1+$L1),$s2);
	
	
if(
$pos2!==false) return substr($s,$pos1+$L1,$pos2);
	
}
	
return 
'';
}

//function.tblClass: reads the CSS stylesheet and extracts the appropriate class values
$cssFile file_get_contents(THEME."styles.css");
function 
tblClass($tbl){
	
global 
$cssFile;
	
$arrayTables = array("tbl1""tbl2""td");
	
if (
in_array($tbl$arrayTables)){
	
	
$tblClasses strBetween(".".$tbl." {""}"$cssFile);
	
	
$cssClasses explode(";"$tblClasses);
	
	
foreach (
$cssClasses as $class){
	
	
	
if (
strpos($class"ground-color") > 0){
	
	
	
	
$newClass strstr($class"background-color:");
	
	
	
	
$newClass str_replace("background-color:"""$newClass);
	
	
	
	
$tblClass trim($newClass);
	
	
	
}
	
	
}
	
	
return 
trim($tblClass);
	
}
}

//Show the result/implode the classes into an array
$implodeClasses tblClass("tbl1").".".tblClass("tbl2"); //returns "#fdfdfd.#f6f6f6"

//Load the classes into an array and explode them so they can be used separately, then define the colors
$classArr explode("."$implodeClasses);

$tbl1 $classArr['0']; //returns "#fdfdfd"
$tbl2 $classArr['1']; //returns "#f6f6f6"

?>


then apply the $tbl1 and $tbl2 variables in the mouse hover events for HTML, seen above in the 2nd block of code, like:

$mouseout1 " onmouseover=\"this.style.backgroundColor='".$tbl2."'\" onmouseout=\"this.style.backgroundColor='".$tbl1."'\"";
$mouseout2 " onmouseover=\"this.style.backgroundColor='".$tbl1."'\" onmouseout=\"this.style.backgroundColor='".$tbl2."'\"";
?>


as of now, this does work, but its disabled because of file_get_contents is required..


Last blog : phpHaze 1.59.1 in Development
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #13 on: Jul 12, 2007, 10:39:17 pm »

thats a lot of code for a simple thing Smiley

Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=6895
Tags : php css Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: using PHP to extract CSS variables
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2012, 10:08:36 pm





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.