質問

私はPHPを使用します。

私は、自動的に一つにまとめ、すべての私のCSSファイルを配置する方法に取り組んでいます。私は自動的にアップロードするために、CSS-ファイルをロードし、大きい方に保存します。

私のローカルインストールでは、私は削除する必要があるいくつかの@importラインを持っています。

これは、次のようになります:

@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; }

上記のスタイルは、文字列内にある場合、どのように私は最善の方法は、にpreg_replace以上で@インポートラインを置き換えますか?空白ギャップを残していていいだろう。

役に立ちましたか?

解決

この正規表現を経由して、それを処理する必要があります:

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

他のヒント

あなたは簡単にそれぞれの行を反復し、それは@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);
}

それとも、フロントアップ配列にファイルを保存する場合:

$lines = file('/path/to/file.css');
foreach ($lines as $num => $line) {
    if (strpos($line, '@import') !== false) {
        // @import found, skip over line
        continue;
    }
}

str_replace( "@インポート"、 ''、$ strの);

str_replaceを使用してそれらを交換、その後、するpreg_matchを使用して@importsを見つけることが容易であるかもしれません
$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);
}

限りのみすべての@importラインをストリッピング

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

仕事をする必要があります...

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top