本文介绍了php preg_replace注释块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
patern就像这样
/ *评论[评论在这里] * /
/ *不要删除下面的行* /
[我是一个特殊的行,所以我不应该改变]
/ *注释请不要删除上面的行* /
我想删除注释块,而不是查找 / ** /
我想让注释 / * comment [] * /
其中 []
是实际的注释。 / p>
这是为了避免任何包含注释的文本。
这里是注释的条件
p>- 以= >>
/ *注释
开始 - 后跟= =任何
- starts with =>>
/* comment
- followed by =>> anything
- followed by =>>
*/
后跟$ =
* /
> 解决方案
这将删除注释块:
preg_replace('%/ \ * \s + comment \s +。*?\ * /%s','',$ string)
并且这个get也删除了过时的空格:
preg_replace '%/ \s * \ * \s + comment \s +。*?\ * / \s *%s','',$ string)
这里是一个测试脚本:
#! usr / bin / php
<?php
$ string =<<< EOS
/ *注释* /
/ *注释请不要删除以下行* /
[我是特殊行,所以我不应该改变]
/ *注释请不要删除上面的行* /
EOS;
print $ string;
print\\\
--- \\\
;
print preg_replace('%/ \ * \s + comment\s +。*?\ * /%s','',$ string);
print\\\
--- \\\
;
print preg_replace('%/ \s * \ * \s + comment\s +。*?\ * / \s *%s','',$ string);
?>
使用PHP 5.3.4输出:
/ *评论[评论在这里] * /
/ *评论请不要删除下面的行* /
[我是一个特殊的行,所以我不应该
/ *注释请不要删除上面的行* /
---
[我是一个特殊的行,所以我不应该改变]
---
[我是特殊行,所以我不应该改变]
the patern is like so
/* 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 would like to remove the comment blocks, but instead of looking for
/**/
I would like the comment to be/* comment [] */
where[]
is the actual comment.This is to avoid any text that should include comments.
so here are the conditions of the comment
解决方案This removes the comment blocks:
preg_replace('%/\*\s+comment\s+.*?\*/%s', '', $string)
And this get's rid of obsolete whitespace as well:
preg_replace('%/\s*\*\s+comment\s+.*?\*/\s*%s', '', $string)
Here's a test script:
#!/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); ?>
Output with 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 ]
这篇关于php preg_replace注释块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!