phpcssregexnewlinecurly-braces

Add newline after each closing curly brace


I'm using a bit of code I found on the internet somewhere to compress my css files and it works great, however there is one regex issue that is causing a bit of trouble.

$css = preg_replace('/([\\w#\\.\\*]+)\\s+([\\w#\\.\\*]+){/', "$1\n$2{", $css);

This statement is supposed to separate multiple css rules on a single line into one css rule per line.

For example:

block_1 .block_2 {color:red} .block_3 .block_4 {color:blue}

Should become:

.block_1 .block_2 {color:red} 
.block_3 .block_4 {color:blue}

But what it does is something like:

.block_1 
.block_2 {color:red} .block_3 
.block_4 {color:blue}

It does the same thing with rules such as

.block_1 p {} div.x div {}

Can someone take a quick look at that regex line? Regex is NOT my forte :)


Solution

  • Perhaps it's just me, but wouldn't a simple string replace work too?

    Just replace } with }\n and you're done.