使用PHP删除注释中的CSS

时间:2017-10-16 22:15:37

标签: php regex

我们说我有一个这样的字符串:

$css = '.class {
    color: red;
}

/* Start Comment */
.replace {
    content: "me";
}
/* End Comment */';

如何删除评论中的所有内容,包括评论本身?

我想要这个结果:

.class {
    color: red;
}

我尝试过这样的事情,但它没有做到:

$css = preg_replace( '#(/* Start Comment */).*?(/* End Comment */)#', '', $css );

我也试过这个:https://stackoverflow.com/a/13824617/2391422

我在这里错过了一些非常明显的东西吗?

真的很感激任何反馈 - 正则表达式是我需要进一步研究的东西。

1 个答案:

答案 0 :(得分:1)

您可以通过添加DOTALL修饰符s来使用此代码 http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php

$css = preg_replace( '#(/\\* Start Comment \\*/).*?(/\\* End Comment \\*/)#s', '', $css );

此外,应转义*以获得所需效果。 <{1}}很好,因为你正在使用另一个分隔符。