2013-12-13 43 views
0

我要读一个CSS文件,并删除其评论,所以我决定用preg_replace函数在PHP:取下CSS文件的意见

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

回答

1

.*更改为.*?

echo preg_replace('#/\*.*?\*/#s', '', $original); 

这只会删除/*为最接近的*/而不是最远。

+0

就像一个魅力。感谢您的快速和准确的答案。请问,为什么讯问与最接近的匹配?我的意思是,它的'0或1次出场'比赛不是吗? – Carlos

+1

@Carlos不在这种情况下。见http://www.php.net/manual/en/regexp.reference.repetition.php –

0

如下我想接近它:

\/\*((?!\*\/).*?)\*\/ 


\/\*   # Start-of-comment 
((?!\*\/).*?) # Any character 0 or more times, lazy, 
       # without matching an end-of-comment 
\*\/   # End-of-comment 

Demo