patern就是这样
/* 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
*/
答案 0 :(得分:3)
这将删除注释块:
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);
?>
使用PHP 5.3.4输出:
/* 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 ]
答案 1 :(得分:0)
似乎要做的工作:)
preg_replace("(\\/\\*[\s]*?comment[\\d\\D]*?[\s]*?\\*\\/)",'',$str)
我是怎么发现的?
这个网站真是太棒了:)