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
https://forums.phpfreaks.com/topic/36634-how-to-dig-a-css-values/
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..

 

<?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

 

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

 

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>

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);

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.