2009-10-17 40 views
4

我正在寫一個快速preg_replace從CSS中去除註釋。 CSS評論通常有這種語法:preg_replace出CSS評論?

/* Development Classes*/ 
/* Un-comment me for easy testing 
    (will make it simpler to see errors) */ 

所以我試圖殺死一切間/ *和* /,就像這樣:

$pattern = "#/\*[^(\*/)]*\*/#"; 
$replace = ""; 
$v = preg_replace($pattern, $replace, $v); 

沒有骰子!它似乎在歪斜正斜槓,因爲如果我將/ s從模式中刪除,我可以將它刪除評論文本。我嘗試了一些簡單的模式,看看我是否可以只輸斜線,但他們回到原來的字符串不變:

$pattern = "#/#"; 
$pattern = "/\//"; 

爲什麼我似乎無法匹配那些斜線任何想法?謝謝!

+1

我不會提供它作爲答案,因爲它不直接適用於這個問題,但有一些很好的工具可以刪除/縮小CSS內容:http://www.minifycss.com/ 只適用於那些可能在這裏試圖重新發明輪子的人:) – AvatarKava 2009-10-17 15:29:27

+0

你應該更好地使用解析器。否則,您將刪除「content:」/ * ... * /「'之類的內容。 – Gumbo 2009-10-17 15:33:56

+0

AvatarKara - 我實際上使用這個腳本: http://code.google.com/p/cssmin/ - 但它並沒有因爲某些原因刪除註釋。我認爲我必須自己寫這一點,但由於其他腳本和答案不起作用,它似乎是一個陌生人正在發生......這是用於EE插件,所以我想知道是否字符串被傳入或環境有我不知道的一些怪癖。感謝您的鏈接! – 2009-10-17 18:16:27

回答

11

這裏有一個解決方案:

$regex = array(
"`^([\t\s]+)`ism"=>'', 
"`^\/\*(.+?)\*\/`ism"=>"", 
"`([\n\A;]+)\/\*(.+?)\*\/`ism"=>"$1", 
"`([\n\A;\s]+)//(.+?)[\n\r]`ism"=>"$1\n", 
"`(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+`ism"=>"\n" 
); 
$buffer = preg_replace(array_keys($regex),$regex,$buffer); 

在Samstyle PHP框架

看到腳本/樣式表預處理器摘自:http://code.google.com/p/samstyle-php-framework/source/browse/trunk/sp.php

csstest.php:

<?php 

$buffer = file_get_contents('test.css'); 

$regex = array(
"`^([\t\s]+)`ism"=>'', 
"`^\/\*(.+?)\*\/`ism"=>"", 
"`([\n\A;]+)\/\*(.+?)\*\/`ism"=>"$1", 
"`([\n\A;\s]+)//(.+?)[\n\r]`ism"=>"$1\n", 
"`(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+`ism"=>"\n" 
); 
$buffer = preg_replace(array_keys($regex),$regex,$buffer); 
echo $buffer; 

?> 

test.css:

/* testing to remove this */ 
.test{} 

輸出csstest.php的:

.test{} 
+0

拿出了一些空格和換行符,但似乎完好無損地留下了所有評論。不過,它絕對看起來像第三行應該刪除評論。莫名其妙。 – 2009-10-17 01:07:21

+0

你好,它適用於我 - 完全。評論全部刪除。 – mauris 2009-10-17 01:53:08

+0

有趣!感謝您的更新測試。我在ExpressionEngine插件中運行這個,所以我想知道是否有一些環境差異導致我的問題。 – 2009-10-17 17:33:49

5

我不相信你可以使用一個否定的字符類中的分組像你那裏。你要使用的是斷言,其中有兩種類型。 「向前看」和「向後看」。

您在尋找英語的模式基本上是「正斜槓,文字通配符,任何沒有跟着正斜槓的字符或任何其他字符通配符,然後是正斜槓或正斜槓不是由文字通配符零次或多次前面,文字外卡,斜線

<?php 

$str = '/* one */ onemore 
/* 
* a 
* b 
**/ 
stuff // single line 
/**/'; 

preg_match_all('#/\*(?:.(?!/)|[^\*](?=/)|(?<!\*)/)*\*/#s', $str, $matches); 
print_r($matches); 

?> 
+0

感謝您的深入解釋。我無法讓它在我使用它的文件中工作,但它在隔離時完美工作,所以我懷疑我有更多的問題需要弄清楚...... – 2009-10-17 18:10:34

0

只是爲了好玩(當然小項目),我做了一個非正則表達式版本這樣的代碼(我希望它更快):

function removeCommentFromCss($textContent) 
{ 
    $clearText = ""; 
    $charsInCss = strlen($textContent); 
    $searchForStart = true; 
    for($index = 0; $index < $charsInCss; $index++) 
    { 
     if ($searchForStart) 
     { 
      if ($textContent[ $index ] == "/" && (($index + 1) < $charsInCss) && $textContent[ $index + 1 ] == "*") 
      { 
       $searchForStart = false; 
       continue; 
      } 
      else 
      { 
       $clearText .= $textContent[ $index ]; 
      } 
     } 
     else 
     { 
      if ($textContent[ $index ] == "*" && (($index + 1) < $charsInCss) && $textContent[ $index + 1 ] == "/") 
      { 
       $searchForStart = true; 
       $index++; 
       continue; 
      } 
     } 
    } 
    return $clearText; 
}