Search code examples
phpcssstylesheetpreg-replacepreg-match

Replace text between @import and \n


I use PHP.

I'm working on a way to automatically put together all my CSS files into one. I automatically load the CSS-files and then saves them to a larger one, for upload.

In my local installation I have some @import lines that needs to be removed.

It looks like this:

@import url('css/reset.css');
@import url('css/grid.css');
@import url('css/default.css');
@import url('css/header.css');
@import url('css/main.css');
@import url('css/sidebar.css');
@import url('css/footer.css');
body { font: normal 0.75em/1.5em Verdana; color: #333; }

If the style above is within a string, how do I the best way replace the @import-lines with preg_replace or better? It would be nice to not leave a whitespace gap.


Solution

  • This should handle it via regex:

    preg_replace('/\s*@import.*;\s*/iU', '', $text);