Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从css文件中删除注释_Php_Regex - Fatal编程技术网

Php 从css文件中删除注释

Php 从css文件中删除注释,php,regex,Php,Regex,我必须阅读css文件并删除其注释,因此我决定使用php中的preg\u replace函数: $original = ' /* foo */ .test, input[type="text"]{ color: blue; } /* bar */ a:hover{ text-decoration: underline; color: red; background: #FFF; }'; echo preg_replace('/\/\*.*\*\//s', '', $original); 问题

我必须阅读css文件并删除其注释,因此我决定使用php中的
preg\u replace
函数:

$original = '
/*
foo
*/
.test, input[type="text"]{ color: blue; }

/* bar */
a:hover{
text-decoration: underline;
color: red;
background: #FFF;
}';

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

问题是它丢失了行
。测试,输入[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);
它不适用于
//此处的注释
,但它适用于
/*此处的注释*/
,甚至适用于多行

对于此处的
//注释
,请使用以下注释:

$buffer = preg_replace('/\/\*.*?\*\//s', '', $buffer);

就像一个符咒。谢谢你快速准确的回答。我可以问一下,为什么审讯是最接近的?我的意思是,这是“0或1眼”匹配,不是吗?@Carlos在这种情况下不是。看见