Jump to content

How to dig a css values!


kucing

Recommended Posts

Hi every one..

 

I got a little question about how do I dig css values from a css file..

 

Like in my site my members are allowed to change the background of there page so my google ads are having a tought time with background colors as the default I set was white..

 

I need a function or php script or a way how to sort this out..

 

An example what kinda css codes are being used at my site..

 

<!-- Css -->

<style type="text/css">
{ Background Properties }
table, tr, td { background-color:transparent; border:none; border-width:0;}
body {
background-color:444444;
background-attachment: fixed;
background-position:bottom left;
background-repeat:no-repeat;
border-color:EEEEEE;
border-width:2px ;
border-style: solid;
scrollbar-face-color:000000;
scrollbar-highlight-color:BBBBBB;
scrollbar-3dlight-color:000000;
scrollbar-shadow-color:000000;
scrollbar-darkshadow-color:000000;
scrollbar-arrow-color:EEEEEE;
scrollbar-track-color:EEEEEE;
 }

{ Table Properties }
table table { border: 0px }
table table table table{border:0px}
table table table {
border-style:solid;
border-width:2px;
border-color:CCCCCC;
background-color:666666;
   		}

{ Text Properties }
table, tr, td, li, p, div {  color:EEEEEE;      } 
.btext {  color:EEEEEE;      } 
.blacktext10 {  color:EEEEEE;      } 
.blacktext12 {  color:EEEEEE;      } 
.lightbluetext8 {  color:EEEEEE;      } 
.orangetext15 {  color:EEEEEE;      } 
.redtext {  color:EEEEEE;      } 
.redbtext {  color:EEEEEE;      } 
.text {  color:EEEEEE;      } 
.whitetext12 {  color:EEEEEE;      } 
a:active, a:visited, a:link {  color:EEEEEE;      } 
a:hover {  color:EEEEEE;      } 
a.navbar:active, a.navbar:visited, a.navbar:link {  color:EEEEEE;      } 
a.navbar:hover {  color:EEEEEE;      } 
a.redlink:active, a.redlink:visited, a.redlink:link {  color:EEEEEE;      } 
a.redlink:hover {  color:EEEEEE;      } 
.nametext {  color:EEEEEE;      }
</style>

<!-- /Css -->

 

Now if you see the background-color value is define three times so the only background value I would need will be from body..

 

body {
background-color:444444;
background-attachment: fixed;
background-position:bottom left;
background-repeat:no-repeat;
border-color:EEEEEE;
border-width:2px ;
border-style: solid;
scrollbar-face-color:000000;
scrollbar-highlight-color:BBBBBB;
scrollbar-3dlight-color:000000;
scrollbar-shadow-color:000000;
scrollbar-darkshadow-color:000000;
scrollbar-arrow-color:EEEEEE;
scrollbar-track-color:EEEEEE;
 }

 

Any Help at all is welcome.. :)

Link to comment
Share on other sites

Thanks for reply..

 

Actually I only need one value from the css file which is the background-color value..

 

And it should only come from

 

body {
background-color:444444;
}

 

which will be "444444"

 

 

So my I can re-define the google adsense background value..

 

Link to comment
Share on other sites

<?php
$array = file("ad.txt");
$result = array_keys($array, "background-color:");//gets the line number
$linenumber = $result[0];//linenumber
$line2 = explode(":", $array[$linenumber]);
$line2 = str_replace(";","",$line2);
echo $line2;
?>

Does this do what you want?

Ted

 

Link to comment
Share on other sites

Hi ted_chou12,

 

This isn't working and also in css codes there will be many background-color but I only need one background-color value which will be located in body{}

 

Regards,

K

 

 

<?php
$array = file("ad.txt");
$result = array_keys($array, "background-color:");//gets the line number
$linenumber = $result[0];//linenumber
$line2 = explode(":", $array[$linenumber]);
$line2 = str_replace(";","",$line2);
echo $line2;
?>

Does this do what you want?

Ted

 

Link to comment
Share on other sites

Oh this is nice.. :)

 

But with a little problem..

It select body{background-color:444444;

 

So is that possible to make this regexp something like this

(?<=body\{\s)(?<=background-color:+)(?=;\s\))

 

which is not working as I am still noob in regexp and I took this from

http://www.phpfreaks.com/forums/index.php/topic,123402.0.html

 

This actuall idea here is that it only should take the background-color:"value"

 

Thanks :)

 

Assuming your CSS is in the variable $data:

 

<pre>
<?php
preg_match('/body\s+\{[^}]+background-color\s*:\s*(.+?);/s', $data, $matches);
print_r($matches);
?>
</pre>

Link to comment
Share on other sites

Lookbehinds have to be of fixed-length, which doesn't mesh well with a flexible syntax like CSS. Also, keep in mind that $matches will always show you the entire match in addition to the captured matches. If you need to replace this value, capture the surrounding information and put it back:

 

$data = preg_replace('/(body\s+\{[^}]+?background-color\s*:\s*)([^;]+)/s', '\1new_color', $data);
echo htmlspecialchars($data);

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.