0

I'm trying to parse a CSS file with the Raphael Schweikert's lib Sabberworm\PHP-CSS-PARSE

But I'm blocked with object manipulations. What I'm trying to do is to convert this css file:

.header {
  background: #000;
}

.footer, .wrapper {
  background: #ebebeb;
}

In a PHP/JSON like format:

 '.header'  => "background: #000;",
 '.footer'  => "background: #ebebeb;",
 '.wrapper' => "background: #ebebeb;"

What i did:

<?php

require 'vendor/autoload.php';
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('style.css'));
$oCssDocument = $oCssParser->parse();
$selectors=$oCssDocument->getAllRuleSets();
...

but how to loop after ? The structure of the object is not easy for a neewbe like me...

2 Answers 2

0

(This should be a comment, but with the code this is better readable).

I'm not familiar with PHP-CSS-PARSE, but I have one observation: I would assume

.footer .wrapper {
  background: #ebebeb;
}

would become '.footer .wrapper' => "background: #ebebeb;".

If you want

'.footer'  => "background: #ebebeb;",
'.wrapper' => "background: #ebebeb;"

then the CSS would have to be

.footer, .wrapper {
  background: #ebebeb;
}

(notice the comma) or have to have separate rules.

1
  • oups you are right. I missed the coma. My source is effectively .footer, .wrapper { background: #ebebeb; } Commented Apr 23, 2014 at 11:28
0

I was lazy yesterday...

<?php

require 'vendor/autoload.php';

$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('style.css'));
$oCssDocument = $oCssParser->parse();

foreach ($oCssDocument->getAllRuleSets() as $oRuleSet) {

    $sSelector = $oRuleSet->getSelectors();
    foreach ($sSelector as $currentSelector) {
            $sSelector = $currentSelector->getSelector();
            echo "\r\n\"$sSelector\" = \"";
            foreach( $oRuleSet->getRules() as $sRules) {
                echo "$sRules ";
            }
            echo "\"";
    }
}

wich renders

".header" = "background: #000; "
".footer" = "background: #ebebeb; "
".wrapper" = "background: #ebebeb; "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.