Advertisement
Guest User

Untitled

a guest
Oct 11th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. <?php
  2. class cssparser {
  3. var $css;
  4. var $html;
  5.  
  6. function cssparser($html = true) {
  7. // Register "destructor"
  8. register_shutdown_function(array(&$this, "finalize"));
  9. $this->html = ($html != false);
  10. $this->Clear();
  11. }
  12.  
  13. function finalize() {
  14. unset($this->css);
  15. }
  16.  
  17. function Clear() {
  18. unset($this->css);
  19. $this->css = array();
  20. if($this->html) {
  21. }
  22. }
  23.  
  24. function SetHTML($html) {
  25. $this->html = ($html != false);
  26. }
  27.  
  28. function Add($key, $codestr) {
  29. $key = strtolower($key);
  30. $codestr = strtolower($codestr);
  31. if(!isset($this->css[$key])) {
  32. $this->css[$key] = array();
  33. }
  34. $codes = explode(";",$codestr);
  35. if(count($codes) > 0) {
  36. foreach($codes as $code) {
  37. $code = trim($code);
  38. list($codekey, $codevalue) = explode(":",$code);
  39. if(strlen($codekey) > 0) {
  40. $this->css[$key][trim($codekey)] = trim($codevalue);
  41. }
  42. }
  43. }
  44. }
  45.  
  46. function Get($key, $property) {
  47. $key = strtolower($key);
  48. $property = strtolower($property);
  49.  
  50. list($tag, $subtag) = explode(":",$key);
  51. list($tag, $class) = explode(".",$tag);
  52. list($tag, $id) = explode("#",$tag);
  53. $result = "";
  54. foreach($this->css as $_tag => $value) {
  55. list($_tag, $_subtag) = explode(":",$_tag);
  56. list($_tag, $_class) = explode(".",$_tag);
  57. list($_tag, $_id) = explode("#",$_tag);
  58.  
  59. $tagmatch = (strcmp($tag, $_tag) == 0) | (strlen($_tag) == 0);
  60. $subtagmatch = (strcmp($subtag, $_subtag) == 0) | (strlen($_subtag) == 0);
  61. $classmatch = (strcmp($class, $_class) == 0) | (strlen($_class) == 0);
  62. $idmatch = (strcmp($id, $_id) == 0);
  63.  
  64. if($tagmatch & $subtagmatch & $classmatch & $idmatch) {
  65. $temp = $_tag;
  66. if((strlen($temp) > 0) & (strlen($_class) > 0)) {
  67. $temp .= ".".$_class;
  68. } elseif(strlen($temp) == 0) {
  69. $temp = ".".$_class;
  70. }
  71. if((strlen($temp) > 0) & (strlen($_subtag) > 0)) {
  72. $temp .= ":".$_subtag;
  73. } elseif(strlen($temp) == 0) {
  74. $temp = ":".$_subtag;
  75. }
  76. if(isset($this->css[$temp][$property])) {
  77. $result = $this->css[$temp][$property];
  78. }
  79. }
  80. }
  81. return $result;
  82. }
  83.  
  84. function GetSection($key) {
  85. $key = strtolower($key);
  86.  
  87. list($tag, $subtag) = explode(":",$key);
  88. list($tag, $class) = explode(".",$tag);
  89. list($tag, $id) = explode("#",$tag);
  90. $result = array();
  91. foreach($this->css as $_tag => $value) {
  92. list($_tag, $_subtag) = explode(":",$_tag);
  93. list($_tag, $_class) = explode(".",$_tag);
  94. list($_tag, $_id) = explode("#",$_tag);
  95.  
  96. $tagmatch = (strcmp($tag, $_tag) == 0) | (strlen($_tag) == 0);
  97. $subtagmatch = (strcmp($subtag, $_subtag) == 0) | (strlen($_subtag) == 0);
  98. $classmatch = (strcmp($class, $_class) == 0) | (strlen($_class) == 0);
  99. $idmatch = (strcmp($id, $_id) == 0);
  100.  
  101. if($tagmatch & $subtagmatch & $classmatch & $idmatch) {
  102. $temp = $_tag;
  103. if((strlen($temp) > 0) & (strlen($_class) > 0)) {
  104. $temp .= ".".$_class;
  105. } elseif(strlen($temp) == 0) {
  106. $temp = ".".$_class;
  107. }
  108. if((strlen($temp) > 0) & (strlen($_subtag) > 0)) {
  109. $temp .= ":".$_subtag;
  110. } elseif(strlen($temp) == 0) {
  111. $temp = ":".$_subtag;
  112. }
  113. foreach($this->css[$temp] as $property => $value) {
  114. $result[$property] = $value;
  115. }
  116. }
  117. }
  118. return $result;
  119. }
  120.  
  121. function ParseStr($str) {
  122. $this->Clear();
  123. // Remove comments
  124. $str = preg_replace("/\/\*(.*)?\*\//Usi", "", $str);
  125. // Parse this damn csscode
  126. $parts = explode("}",$str);
  127. if(count($parts) > 0) {
  128. foreach($parts as $part) {
  129. list($keystr,$codestr) = explode("{",$part);
  130. $keys = explode(",",trim($keystr));
  131. if(count($keys) > 0) {
  132. foreach($keys as $key) {
  133. if(strlen($key) > 0) {
  134. $key = str_replace("\n", "", $key);
  135. $key = str_replace("\\", "", $key);
  136. $this->Add($key, trim($codestr));
  137. }
  138. }
  139. }
  140. }
  141. }
  142. //
  143. return (count($this->css) > 0);
  144. }
  145.  
  146. function Parse($filename) {
  147. $this->Clear();
  148. if(file_exists($filename)) {
  149. return $this->ParseStr(file_get_contents($filename));
  150. } else {
  151. return false;
  152. }
  153. }
  154.  
  155. function GetCSS() {
  156. $result = "";
  157. foreach($this->css as $key => $values) {
  158. $result .= $key." {\n";
  159. foreach($values as $key => $value) {
  160. $result .= " $key: $value;\n";
  161. }
  162. $result .= "}\n\n";
  163. }
  164. return $result;
  165. }
  166. }
  167. ?>
  168.  
  169.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement