php preg_replace评论块

时间:2011-03-22 11:55:26

标签: php preg-replace comments

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 [] */,其中[]是实际评论。

这是为了避免任何应包含评论的文字。

以下是评论的条件

  1. 以=>>开头/* comment
  2. 后跟=>>任何
  3. 后跟=>> */

2 个答案:

答案 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)

我是怎么发现的?

这个网站真是太棒了:)

http://txt2re.com/index.php3