[英]Remove comments from css file
我必須閱讀一個 css 文件並刪除它的注釋,所以我決定使用 php 中的preg_replace
函數:
$original = '
/*
foo
*/
.test, input[type="text"]{ color: blue; }
/* bar */
a:hover{
text-decoration: underline;
color: red;
background: #FFF;
}';
echo preg_replace('/\/\*.*\*\//s', '', $original);
問題是它丟失了.test, input[type="text"]{ color: blue; }
.test, input[type="text"]{ color: blue; }
更改.*
至.*?
.
echo preg_replace('#/\*.*?\*/#s', '', $original);
這只會刪除/*
到最近的*/
而不是最遠的。
我會按如下方式處理它:
\/\*((?!\*\/).*?)\*\/
\/\* # Start-of-comment
((?!\*\/).*?) # Any character 0 or more times, lazy,
# without matching an end-of-comment
\*\/ # End-of-comment
試試這個:
$buffer = preg_replace('/\*[^*]*\*+([^/][^*]*\*+)*/', '', $buffer);
它不適用於//Comments here
,但它適用於/* Comment here */
的其余部分,甚至適用於多行。
對於//Comments here
,使用這個:
$buffer = preg_replace('/\/\*.*?\*\//s', '', $buffer);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.