0

The input is a multi-line string such as this:

    table {
      border:0;
      border-spacing:0;
      border-collapse:collapse;
      margin-left:auto;                 // 'align:center' equivalent
      margin-right:auto;
    }

The output is a single-line string with "extra" whitespace & comments removed, e.g.:

    table { border:0; border-spacing:0; border-collapse:collapse; margin-left:auto;}

One way to do this with Perl is:

    #! perl -w
    use strict;

    my $css = <<CSS;
    table {
      border:0;
      border-spacing:0;
      border-collapse:collapse;
      margin-left:auto;                 // 'align:center' equivalent
      margin-right:auto;
    }
    CSS

    $css =~ s/\s{2,}/ /g;   # globally replace 2 or more whitespace with 1 space
    $css =~ s|\s+//.+\n||g; # globally replace C comment with empty string
    $css =~ s|\n||g;        # globally replace newline with empty string

    print $css;

I tried doing something similar in PHP but it does nothing to the input:

    <?php

    define("CSS_TABLE",
    "table {
      border:0;
      border-spacing:0;
      border-collapse:collapse;
      margin-left:auto;              // 'align:center' equivalent
      margin-right:auto;
    }");

    $css = CSS_TABLE;

    preg_replace("/\s\s+/", ' ', $css);
    preg_replace("/\s+\/\/.+\n/", '', $css);
    preg_replace("/\n/", '', $css);

    echo("CSS-min: $css\n\n");
    ?>

Note: the 'define' isn't the issue because I also used a 'here' doc -- no joy either way. I'm showing the 'define' instead of a 'here' doc (as in the Perl example) because the existing PHP code uses it (and a whole bunch of others!).

What am I doing wrong?

Scavokovich
  • 89
  • 2
  • 9

2 Answers2

1

Try like this.

First of all you need to remove comments

define("CSS_TABLE",
    "table {
      border:0;
      border-spacing:0;
      border-collapse:collapse;
      margin-left:auto;              // 'align:center' equivalent
      margin-right:auto;
    }");

    $css = CSS_TABLE;

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

    echo("CSS-min: $css\n\n");

Result is

CSS-min: table { border:0; border-spacing:0; border-collapse:collapse; margin-left:auto; margin-right:auto; }
Winston
  • 1,758
  • 2
  • 17
  • 29
  • Thank you, Winston! I like your .* vs. my .+ for comments since yours will take care of just // – Scavokovich Feb 23 '13 at 09:39
  • @Scavokovich Yes. my code will care of about // when comments will be empty. Because when will be removed newlines // will comment out css code. – Winston Feb 23 '13 at 09:44
  • 2
    Would `"#//.*#"` not strip out any absolute URLs? For instance `background-image : url('http://mysite.com/file.png');` would become `background-image : url('http:` – kittycat Feb 23 '13 at 09:46
0

Try this

define("CSS_TABLE",
"table {
  border:0;
  border-spacing:0;
  border-collapse:collapse;
  margin-left:auto;              // 'align:center' equivalent
  margin-right:auto;
}");

$css = CSS_TABLE;
$css = preg_replace("#//.*#", '', $css);
$css = preg_replace("/\\s/", ' ', $css);
$css = preg_replace("/\\s+/", ' ', $css);
echo("CSS-min: $css");