5, December 2008

using PHP to extract CSS variables - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ 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

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



« on: Jul 11, 2007, 10: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

Visit through proxy Visit through proxy Visit through proxy

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



« Reply #1 on: Jul 11, 2007, 11: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 Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #2 on: Jul 11, 2007, 11: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?

Visit through proxy Visit through proxy Visit through proxy

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



« Reply #3 on: Jul 11, 2007, 11: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 Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #4 on: Jul 12, 2007, 09: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

Visit through proxy Visit through proxy Visit through proxy

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



« Reply #5 on: Jul 12, 2007, 05: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..

Visit through proxy Visit through proxy Visit through proxy

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



« Reply #6 on: Jul 12, 2007, 05: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 Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6487
39754 credits
Members referred : 374


It's time to use PHP5!


« Reply #7 on: Jul 12, 2007, 08:34:45 PM »

yeah tell us your secrets Smiley


Last blog : Just a better Internet portal provided by Google
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #8 on: Jul 12, 2007, 08: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

Visit through proxy Visit through proxy Visit through proxy

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


It's time to use PHP5!


« Reply #9 on: Jul 12, 2007, 10:32:02 PM »

maybe you can use inline styles plus dyn. values instead


Last blog : Just a better Internet portal provided by Google
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #10 on: Jul 12, 2007, 10: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

Visit through proxy Visit through proxy Visit through proxy

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


It's time to use PHP5!


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

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


Last blog : Just a better Internet portal provided by Google
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #12 on: Jul 12, 2007, 11: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..

Visit through proxy Visit through proxy Visit through proxy

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


It's time to use PHP5!


« Reply #13 on: Jul 12, 2007, 11:39:17 PM »

thats a lot of code for a simple thing Smiley


Last blog : Just a better Internet portal provided by Google
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....

Topic sponsors:
Get a permanent link here for $1.99!


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?
Dec 05, 2008, 06:24:42 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!


Forum Statistics
Total Posts: 38.003
Total Topics: 7.685
Total Members: 4.472
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: xeroxss

28 Guests, 5 Users online :

12 users online today:



Readers

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