入力は、次のような複数行の文字列です。
table {
border:0;
border-spacing:0;
border-collapse:collapse;
margin-left:auto; // 'align:center' equivalent
margin-right:auto;
}
出力は、「余分な」空白とコメントが削除された単一行の文字列です。例:
table { border:0; border-spacing:0; border-collapse:collapse; margin-left:auto;}
Perl でこれを行う 1 つの方法は次のとおりです。
#! 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;
PHPで同様のことを試みましたが、入力には何もしません:
<?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");
?>
注:「ここ」ドキュメントも使用したため、「定義」は問題ではありません-どちらにしても喜びはありません。既存の PHP コード (および他の多くのコード) で使用されているため、(Perl の例のように)「here」ドキュメントの代わりに「define」を表示しています。
私は何を間違っていますか?