You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+65-41Lines changed: 65 additions & 41 deletions
Original file line number
Diff line number
Diff line change
@@ -11,22 +11,28 @@ A Parser for CSS Files written in PHP. Allows extraction of CSS files into a dat
11
11
12
12
Add php-css-parser to your composer.json
13
13
14
-
{
15
-
"require": {
16
-
"sabberworm/php-css-parser": "*"
17
-
}
18
-
}
14
+
```json
15
+
{
16
+
"require": {
17
+
"sabberworm/php-css-parser": "*"
18
+
}
19
+
}
20
+
```
19
21
20
22
### Extraction
21
23
22
24
To use the CSS Parser, create a new instance. The constructor takes the following form:
23
25
24
-
new Sabberworm\CSS\Parser($sText);
26
+
```php
27
+
new Sabberworm\CSS\Parser($sText);
28
+
```
25
29
26
30
To read a file, for example, you’d do the following:
27
31
28
-
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'));
29
-
$oCssDocument = $oCssParser->parse();
32
+
```php
33
+
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'));
34
+
$oCssDocument = $oCssParser->parse();
35
+
```
30
36
31
37
The resulting CSS document structure can be manipulated prior to being output.
32
38
@@ -36,21 +42,27 @@ The resulting CSS document structure can be manipulated prior to being output.
36
42
37
43
The charset option is used only if no @charset declaration is found in the CSS file. UTF-8 is the default, so you won’t have to create a settings object at all if you don’t intend to change that.
To have the parser choke on invalid rules, supply a thusly configured Sabberworm\CSS\Settings object:
45
53
46
-
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'), Sabberworm\CSS\Settings::create()->beStrict());
54
+
```php
55
+
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'), Sabberworm\CSS\Settings::create()->beStrict());
56
+
```
47
57
48
58
#### Disable multibyte functions
49
59
50
60
To achieve faster parsing, you can choose to have PHP-CSS-Parser use regular string functions instead of `mb_*` functions. This should work fine in most cases, even for UTF-8 files, as all the multibyte characters are in string literals. Still it’s not recommended to use this with input you have no control over as it’s not thoroughly covered by test cases.
$oRuleSet->removeRule('font-'); //Note that the added dash will make this remove all rules starting with font- (like font-size, font-weight, etc.) as well as a potential font-rule
143
-
$oRuleSet->removeRule('cursor');
144
-
}
155
+
```php
156
+
$oParser = new Sabberworm\CSS\Parser($sText);
157
+
$oCss = $oParser->parse();
158
+
foreach($oCss->getAllRuleSets() as $oRuleSet) {
159
+
$oRuleSet->removeRule('font-'); //Note that the added dash will make this remove all rules starting with font- (like font-size, font-weight, etc.) as well as a potential font-rule
160
+
$oRuleSet->removeRule('cursor');
161
+
}
162
+
```
145
163
146
164
### Output
147
165
148
166
To output the entire CSS document into a variable, just use `->render()`:
149
167
150
-
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'));
151
-
$oCssDocument = $oCssParser->parse();
152
-
print $oCssDocument->render();
168
+
```php
169
+
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'));
170
+
$oCssDocument = $oCssParser->parse();
171
+
print $oCssDocument->render();
172
+
```
153
173
154
174
If you want to format the output, pass an instance of type `Sabberworm\CSS\OutputFormat`:
0 commit comments