php preg_替换注释块
父亲是这样的php preg_替换注释块,php,preg-replace,comments,Php,Preg Replace,Comments,父亲是这样的 /* comment [comment goes here] */ /* comment please do not delete the lines below */ [I am a special line so I should not be changed ] /* comment please do not delete the line above */ 我想删除注释块,但不是查找/***/,而是希望注释为/*comment[]*/,其中[]是实际注释 这是为了避免任
/* comment [comment goes here] */
/* comment please do not delete the lines below */
[I am a special line so I should not be changed ]
/* comment please do not delete the line above */
我想删除注释块,但不是查找/***/
,而是希望注释为/*comment[]*/
,其中[]
是实际注释
这是为了避免任何应该包含注释的文本
下面是评论的条件
/*注释开始
*/
这将删除注释块:
preg_replace('%/\*\s+comment\s+.*?\*/%s', '', $string)
这也消除了过时的空白:
preg_replace('%/\s*\*\s+comment\s+.*?\*/\s*%s', '', $string)
下面是一个测试脚本:
#!/usr/bin/php
<?php
$string = <<<EOS
/* comment [comment goes here] */
/* comment please do not delete the lines below */
[I am a special line so I should not be changed ]
/* comment please do not delete the line above */
EOS;
print $string;
print "\n---\n";
print preg_replace('%/\*\s+comment\s+.*?\*/%s', '', $string);
print "\n---\n";
print preg_replace('%/\s*\*\s+comment\s+.*?\*/\s*%s', '', $string);
?>
似乎在做这项工作:)
preg\u replace(“(\\/\\*[\s]*?注释[\\d\\d]*?[\s]*?\\*\*\/),”,“$str)
我怎么知道的
这个网站太棒了:)
你尝试了多少?我尝试了一些事情,我的正则表达式知识就是=看着一只猴子试图找出它的反射is@val:您使用的是什么版本的PHP?这两个代码段都在PHP5.3.4上经过测试。(我已经编辑了答案并添加了完整的测试脚本和输出。)@val:对不起,我误解了你问题中的方括号。显然,它们不是注释的一部分,但您可以使用它们来标记占位符。我已经相应地修复了上面的模式。但是,您可以看出这是一个生成的表达式<代码>[\s]*?:方括号已过时,问号(贪婪)和一个字符如下
[\\d\\d]*
:这意味着“任何数字或非数字”,因此它相当于*
。它可能有用,但很难阅读。
/* comment [comment goes here] */
/* comment please do not delete the lines below */
[I am a special line so I should not be changed ]
/* comment please do not delete the line above */
---
[I am a special line so I should not be changed ]
---
[I am a special line so I should not be changed ]