如何解决php preg_replace注释块
| 模式就像这样/* 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
后跟= >>任何内容
后跟= >>*/
解决方法
这将删除注释块:
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 ]
,似乎做好了工作:)
preg_replace(\"(\\\\/\\\\*[\\s]*?comment[\\\\d\\\\D]*?[\\s]*?\\\\*\\\\/)\",$str)
我怎么发现的?
这个网站真是太神奇了:)
http://txt2re.com/index.php3
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。