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.. Link to comment https://forums.phpfreaks.com/topic/36634-how-to-dig-a-css-values/ 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 Link to comment https://forums.phpfreaks.com/topic/36634-how-to-dig-a-css-values/#findComment-174577 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.. Link to comment https://forums.phpfreaks.com/topic/36634-how-to-dig-a-css-values/#findComment-174587 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 Link to comment https://forums.phpfreaks.com/topic/36634-how-to-dig-a-css-values/#findComment-174594 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 Link to comment https://forums.phpfreaks.com/topic/36634-how-to-dig-a-css-values/#findComment-174658 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 Link to comment https://forums.phpfreaks.com/topic/36634-how-to-dig-a-css-values/#findComment-174665 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. Link to comment https://forums.phpfreaks.com/topic/36634-how-to-dig-a-css-values/#findComment-174669 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 Link to comment https://forums.phpfreaks.com/topic/36634-how-to-dig-a-css-values/#findComment-174670 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 Link to comment https://forums.phpfreaks.com/topic/36634-how-to-dig-a-css-values/#findComment-174719 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> Link to comment https://forums.phpfreaks.com/topic/36634-how-to-dig-a-css-values/#findComment-174737 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> Link to comment https://forums.phpfreaks.com/topic/36634-how-to-dig-a-css-values/#findComment-174778 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); Link to comment https://forums.phpfreaks.com/topic/36634-how-to-dig-a-css-values/#findComment-174810 Share on other sites More sharing options...
kucing Posted February 6, 2007 Author Share Posted February 6, 2007 Thanks alot effigy It works perfectly.. Link to comment https://forums.phpfreaks.com/topic/36634-how-to-dig-a-css-values/#findComment-178227 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.