code_junkie

Parsing CSS by regex

Nov 11th, 2011
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. (?<selector>[A-Za-z]+[s]*)[s]*{[s]*((?<properties>[A-Za-z0-9-_]+)[s]*:[s]*(?<values>[A-Za-z0-9#, ]+);[s]*)*[s]*}
  2.  
  3. body { background: #f00; font: 12px Arial; }
  4.  
  5. Array(
  6. [0] => Array(
  7. [0] => body { background: #f00; font: 12px Arial; }
  8. [selector] => Array(
  9. [0] => body
  10. )
  11. [1] => Array(
  12. [0] => body
  13. )
  14. [2] => font: 12px Arial;
  15. [properties] => Array(
  16. [0] => font
  17. )
  18. [3] => Array(
  19. [0] => font
  20. )
  21. [values] => Array(
  22. [0] => 12px Arial
  23. [1] => background: #f00
  24. )
  25. [4] => Array(
  26. [0] => 12px Arial
  27. [1] => background: #f00
  28. )
  29. )
  30. )
  31.  
  32. Array(
  33. [0] => Array
  34. (
  35. [0] => body { background: #f00; font: 12px Arial; }
  36. [selector] => body
  37. [1] => body
  38. [2] => font: 12px Arial;
  39. [properties] => font
  40. [3] => font
  41. [values] => 12px Arial
  42. [4] => 12px Arial
  43. )
  44. )
  45.  
  46. /([^{])s*{s*([^}])s*}/
  47.  
  48. $cssp = new cssparser;
  49. $cssp -> ParseStr("body { background: #f00;font: 12px Arial; }");
  50. print_r($cssp->css);
  51.  
  52. Array
  53. (
  54. [body] => Array
  55. (
  56. [background] => #f00
  57. [font] => 12px arial
  58. )
  59. )
  60.  
  61. $css_array = array(); // master array to hold all values
  62. $element = explode('}', $css);
  63. foreach ($element as $element) {
  64. // get the name of the CSS element
  65. $a_name = explode('{', $element);
  66. $name = $a_name[0];
  67. // get all the key:value pair styles
  68. $a_styles = explode(';', $element);
  69. // remove element name from first property element
  70. $a_styles[0] = str_replace($name . '{', '', $a_styles[0]);
  71. // loop through each style and split apart the key from the value
  72. $count = count($a_styles);
  73. for ($a=0;$a<$count;$a++) {
  74. if ($a_styles[$a] != '') {
  75. $a_key_value = explode(':', $a_styles[$a]);
  76. // build the master css array
  77. $css_array[$name][$a_key_value[0]] = $a_key_value[1];
  78. }
  79. }
  80. }
  81.  
  82. Array
  83. (
  84. [body] => Array
  85. (
  86. [background] => #f00
  87. [font] => 12px arial
  88. )
  89. )
  90.  
  91. function trimStringArray($stringArray){
  92. $result = array();
  93. for($i=0; $i < count($stringArray); $i++){
  94. $trimmed = trim($stringArray[$i]);
  95. if($trimmed != '') $result[] = $trimmed;
  96. }
  97. return $result;
  98. }
  99. $regExp = '/{|}/';
  100. $rawCssData = preg_split($regExp, $style);
  101.  
  102. $cssArray = array();
  103. for($i=0; $i < count($rawCssData); $i++){
  104. if($i % 2 == 0){
  105. $cssStyle['selectors'] = array();
  106. $selectors = split(',', $rawCssData[$i]);
  107. $cssStyle['selectors'] = trimStringArray($selectors);
  108. }
  109. if($i % 2 == 1){
  110. $attributes = split(';', $rawCssData[$i]);
  111. $cssStyle['attributes'] = trimStringArray($attributes);
  112. $cssArray[] = $cssStyle;
  113. }
  114.  
  115. }
  116. //return false;
  117. echo '<pre>'."n";
  118. print_r($cssArray);
  119. echo '</pre>'."n";
  120.  
  121. (?<selector>(?:(?:[^,{]+),?)*?){(?:(?<name>[^}:]+):?(?<value>[^};]+);?)*?}
Add Comment
Please, Sign In to add comment