kucing Posted February 1, 2007 Share Posted February 1, 2007 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.. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 1, 2007 Share Posted February 1, 2007 by digging css values, do you mean something like these: 444444 fixed no-repeat etc...? or something else? if I am not wrong, you can try explode(":", $string); please correct me and explain your purpose more clearly. Ted Quote Link to comment Share on other sites More sharing options...
kucing Posted February 1, 2007 Author Share Posted February 1, 2007 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.. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 1, 2007 Share Posted February 1, 2007 try: <?php //if this is a text file: $lines = file("ad.txt"); $line2 = explode(":", $lines[1]); $line2 = str_replace(";","",$line2); echo $line2; ?> would this help you? Ted Quote Link to comment Share on other sites More sharing options...
kucing Posted February 1, 2007 Author Share Posted February 1, 2007 Hi ted_chou12.. Thanks but your codes will get all the data but I only need one value which exist in body{} The background-color value.. Regards K Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 1, 2007 Share Posted February 1, 2007 <?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 Quote Link to comment Share on other sites More sharing options...
effigy Posted February 1, 2007 Share Posted February 1, 2007 You can also use Regular Expressions; see my signature. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 1, 2007 Share Posted February 1, 2007 thanks, but i dont find it in your sign. Ted Quote Link to comment Share on other sites More sharing options...
kucing Posted February 1, 2007 Author Share Posted February 1, 2007 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 Quote Link to comment Share on other sites More sharing options...
effigy Posted February 1, 2007 Share Posted February 1, 2007 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> Quote Link to comment Share on other sites More sharing options...
kucing Posted February 1, 2007 Author Share Posted February 1, 2007 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> Quote Link to comment Share on other sites More sharing options...
effigy Posted February 1, 2007 Share Posted February 1, 2007 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); Quote Link to comment Share on other sites More sharing options...
kucing Posted February 6, 2007 Author Share Posted February 6, 2007 Thanks alot effigy It works perfectly.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.