php regex issue
I'm using a bit of code I found on the internet somewhere to compress my css files and i开发者_如何学运维t 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 :)
Perhaps it's just me, but wouldn't a simple string replace work too?
Just replace }
with }\n
and you're done.
Instead of re-inventing the wheel with regex (which is hard, and leads to regex no-one -- not even you -- will understand), you should use an existing tool, that's been tested a lot, and is already mature.
For example, you might want to take a look at the YUI Compressor's CSS minifier
精彩评论