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?