views:

283

answers:

4

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.

A: 

str_replace("@import", '', $str);

Mr-sk
That removes the @import but I need to remove the row. It should remove the information between @import and \n.
Jens Törnell
+1  A: 

You could easily iterate over each line and then determine if it starts with @import.

$handle = @fopen('/path/to/file.css', 'r');
if ($handle) {
    while (!feof($handle)) {
        $line = fgets($handle, 4096);
        if (strpos($line, '@import') !== false) {
            // @import found, skip over line
            continue;
        }
        echo $line;
    }
    fclose($handle);
}

Or if you want to store the file in an array up front:

$lines = file('/path/to/file.css');
foreach ($lines as $num => $line) {
    if (strpos($line, '@import') !== false) {
        // @import found, skip over line
        continue;
    }
}
cballou
It would work but it doesn't feel as the best way to solve it. If I don't find anything better I'll might use this one.
Jens Törnell
Regex is slow, this will allow you to create a new file in linear time assuming you create the output as you are iterating over each file.
cballou
Is regexp slower? Because I only generate the CSS-file on a local host, speed doesn't matter for me. The server loads the uploaded generated file.I will use the preg_replace by Inspire. Thanks anyway!
Jens Törnell
A: 

It might be easier to find the @imports using preg_match, then replace them using str_replace

$str = "<<css data>>";
while (preg_match("/@import\s+url\('([^']+)'\);\s+/", $str, $matches)) {
  $url = $matches[1];
  $text = file_get_contents($url); // or some other way of reading that url
  $str = str_replace($matches[0], $text, $str);
}

As far as only stripping all @import lines:

preg_replace("/@import[^;]+;\s+/g", "", $str);

Should do the job...

gnarf
I found a shorter answer written by Inspire, only one row. Thanks anyway!
Jens Törnell
+3  A: 

This should handle it via regex:

preg_replace('/\s*@import.*;\s*/iU', '', $text);
Inspire
This is if you want to *remove* the lines as you mentioned:"In my local installation I have some @import lines that needs to be removed."
Inspire
Would also replace `@import url('something.css'); body { color: #fff; }` with just a `}`
gnarf
Short as I was hoping for. It works like expected. It removes the lines with @import. Thanks!
Jens Törnell