I want to add my class name with each element in css using php -
i want create css parse type..
parse add class name each element in css.
for example i've following css
.class1 {font-size:10px} .class2 {font-style:italic} body {height:100%} html {width:100%}
after parser this.
.myclass .class1 {font-size:10px} .myclass .class2 {font-style:italic} .myclass body {height:100%} .myclass html {width:100%}
i created function doesn't work when @media queries come in css nested elements.
can provide me css parsers/solutions. tried available css parsers. not enough solution.
for this, recommend sabberworm's php-css-parser. i've used myself , it's fantastic tool. can install via composer adding code beneath composer.json
{ "require": { "sabberworm/php-css-parser": "*" } }
after installing it, it's pretty straight forward. can either pass in css-code variable containing css in string, or reading file specific directory. in example, read directory.
$ocssparser = new sabberworm\css\parser(file_get_contents('style.css')); $ocssdocument = $ocssparser->parse();
now you're ready go. on parser's github, have example of how prepend id selectors. i'll show how class.
$myclass = ".myclass"; foreach($ocssdocument->getalldeclarationblocks() $oblock) { foreach($oblock->getselectors() $oselector) { $oselector->setselector($myclass.' '.$oselector->getselector()); } }
now you've prepended class. want return final product, don't you? that's done using built-in function render()
. this
$oformat = sabberworm\css\outputformat::create()->indentwithspaces(4)->setspacebetweenrules("\n"); print $ocssdocument->render($oformat);
and of course, if want output file, can using file_put_contents()
.
hope helps.
Comments
Post a Comment