-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCSS_Parser.php
37 lines (35 loc) · 989 Bytes
/
CSS_Parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
function css_parser($file)
{
$css = file_get_contents($file);
preg_match_all( '/(?ims)([a-z0-9\s\.\:#_\-@,]+)\{([^\}]*)\}/', $css, $arr);
$result = array();
foreach ($arr[0] as $i => $x){
$selector = trim($arr[1][$i]);
$rules = explode(';', trim($arr[2][$i]));
$rules_arr = array();
foreach ($rules as $strRule)
{
if (!empty($strRule)){
$rule = explode(":", $strRule);
$rules_arr[trim($rule[0])] = trim($rule[1]);
}
}
$selectors = explode(',', trim($selector));
foreach ($selectors as $strSel)
{
$result[$strSel] = $rules_arr;
}
}
return $result;
}
$css = css_parser('CSS_Parser.css');
header('Content-Type: text/plain');
foreach ($css as $key => $value)
{
echo $key."{"."\n";
foreach ($value as $key1 => $value2)
{
echo "\t".html_entity_decode($key1).": ".(html_entity_decode($value2.";".""))."\n";
}
}